Printing chars and their ASCII-code in C
This prints out all ASCII values:
int main()
{
int i;
i=0;
do
{
printf("%d %c \n",i,i);
i++;
}
while(i<=255);
return 0;
}
and this prints out the ASCII value for a given character:
int main()
{
int e;
char ch;
clrscr();
printf("\n Enter a character : ");
scanf("%c",&ch);
e=ch;
printf("\n The ASCII value of the character is : %d",e);
getch();
return 0;
}
How to print ASCII codes for letters?
#include <stdio.h>
void get_ASCII_value(char c)
{
printf("The ASCII value of %c = %d", c, c);
}
int main(void)
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
get_ASCII_value(c);
return 0;
}
To clarify, %d
prints the ASCII value for that specific char
As for your specific case:
#include <stdio.h>
int main()
{
char c,z;
printf("Enter An English upper case letter: ");
scanf("%c", &z);
if (z < 65 || z > 90)
return 0;
for (c = 65; c <= z; c++)
printf("The ASCII value of %c = %d\n", c, c);
return 0;
}
Displaying the ASCII-Code of a char in assembly
From comments
MOV AX, 'A'
MOV BX, 10
DIV BL
MOV AH, 2
INT 21h
This byte-sized division will leave a quotient in AL
and a remainder in AH
.
But the DOS.PrintCharacter function 02h expects its input in the DL
register.
After DIV:
ADD AH, 48
ADD AL, 48
Fine to convert, but you can do it in one go with ADD AX, 3030h
I got this output: ##
Try next code:
mov ax, 'A'
mov bl, 10
div bl
add ax, 3030h
mov dx, ax ; Moving tens "6" to DL for printing and preserving the units in DH
mov ah, 02h ; DOS.PrintCharacter
int 21h
mov dl, dh ; units "5"
mov ah, 02h ; DOS.PrintCharacter
int 21h
Displaying numbers with DOS has a detailed explanation about how to deal with even bigger numbers.
Read and print special ascii characters from file in c
Problem is most likely not in the code but in the way you create your text file. When you copy charters you are using (like ▀█▄) from the web and paste it in the default Windows notepad it's using UTF-8 encoding which is UNICODE and what you need is extended ASCII. So, first off I recommend you to use notepad++ and manually set code page of new text file to something like OEM 852. In notepad++ you do it like this:
Encoding > Character Set > Central European > OEM 852
After that, when you paste your special characters in the notepad++, text file they will be saved as ASCII and not UNICODE.
After you have correctly created text file you can print it to console by calling function that looks like this:
void printMyFile(const char *fileName) {
FILE *file = fopen(fileName, "r");
fseek(file, 0L, SEEK_END);
long size = ftell(file);
fseek(file, 0L, SEEK_SET);
char *buffer = (char *)calloc(size, sizeof(char));
fread(buffer, sizeof(char), size, file);
fclose(file);
printf(buffer);
free(buffer);
}
Additionally if your console still show your characters wrongly you should include Windows.h
#include <Windows.h>
And call function
SetConsoleOutputCP(852);
once before you print anything to the console.
Wish you luck!
EDIT 1: I saw your new edit with your code, using SetConsoleOutputCP(437) is also ok and will work.
C - Print ASCII Value for Each Character in a String
If we need write a code to get ASCII values of all elements in a string, then we need to use "%d"
instead of "%c"
. By doing this %d takes the corresponding ascii value of the following character.
If we need to only print the ascii value of each character in the string. Then this code will work:
#include <stdio.h>
char str[100];
int x;
int main(){
scanf("%s",str);
for(x=0;str[x]!='\0';x++){
printf("%d\n",str[x]);
}
}
To store all corresponding ASCII value of character in a new variable, we need to declare an integer variable and assign it to character. By this way the integer variable stores ascii value of character. The code is:
#include <stdio.h>
char str[100];
int x,ascii;
int main(){
scanf("%s",str);
for(x=0;str[x]!='\0';x++){
ascii=str[x];
printf("%d\n",ascii);
}
}
I hope this answer helped you...../p>
Related Topics
Can't Run Executable Linked with Libc
Setting Up a Cronjob on Google Compute Engine
Shell Script to Kill the Process Listening on Port 3000
What Is a Way to Read Man Pages in Vim Without Using Temporary Files
Postgresql CSV Import from Command Line
Can't Change Tomcat 7 Heap Size
Why Is "Echo Foo | Read a ; Echo $A" Not Working as Expected
Sed Extracting Group of Digits
Linux Kernel Config_Debug_Section_Mismatch Make Errors
Posix Shared Memory and Semaphores Permissions Set Incorrectly by Open Calls
How Do 2 or More Fork System Calls Work
How to Figure Out What Is Using a Linux Kernel Module
What Would Be the Equivalent of Win32 API in Linux
How Use Qt in Visual Studio Code
Bluetooth Low Energy: Use Bluez Stack as a Peripheral (With Custom Services and Characteristics)