在C中打印字符及其ASCII码

如何在C中打印char及其等效的ASCII值?

这将打印出所有ASCII值:

int main() { int i; i=0; do { printf("%d %c \n",i,i); i++; } while(i<=255); return 0; } 

这会打印出给定字符的ASCII值:

 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; } 

试试这个:

 char c = 'a'; // or whatever your character is printf("%c %d", c, c); 

%c是单个字符的格式字符串,%d是数字/整数。 通过将char转换为整数,您将获得ascii值。

使用while循环打印从0到255的所有ascii值。

 #include int main(void) { int a; a = 0; while (a <= 255) { printf("%d = %c\n", a, a); a++; } return 0; } 

单引号(’XXXXXX’)中的字符,当以十进制格式打印时应输出其ASCII值。

 int main(){ printf("D\n"); printf("The ASCII of D is %d\n",'D'); return 0; } 

输出:

 % ./a.out >> D >> The ASCII of D is 68 

没有什么比这更简单了

 #include  int main() { int i; for( i=0 ; i<=255 ; i++ ) /*ASCII values ranges from 0-255*/ { printf("ASCII value of character %c = %d\n", i, i); } return 0; } 

来源: 程序打印所有字符的ASCII值

这将从标准输入读取一行文本,并打印出行中的字符及其ASCII代码:

 #include  void printChars(void) { unsigned char line[80+1]; int i; // Read a text line if (fgets(line, 80, stdin) == NULL) return; // Print the line chars for (i = 0; line[i] != '\n'; i++) { int ch; ch = line[i]; printf("'%c' %3d 0x%02X\n", ch, ch, (unsigned)ch); } } 

void main(){

的printf( “%d”, ‘A’); //我们可以用我们选择的字符替换a来获取其ASCII值//

的getch();

}

 #include"stdio.h" #include"conio.h"//RMVIVEK coding for ascii display values void main() { int rmv; for(rmv=0;rmv<=256;rmv++) if(printf("%c",rmv)) getch(); } 

我正在使用“for”这是我的解决方案:

 using System; using System.Text; class PrintASCIITable { static void Main() { byte symbols = 255; for (int i = 0; i < symbols; i++) { Console.WriteLine((char)i); } } } 
 #include #include void main() { int i; char ch; clrscr(); printf("\t Enter a Value: "); scanf("%c",&ch); i=0; while(i<=ch) { printf("\t %d Is %c \n",i,i); i=i+1; } getch(); } 

这应该适用于任何C系统,不仅仅是基于ASCII或UTF-8的系统:

 printf ( " Q is decimal 81 in ASCII\n" ); 

你确实要求一个炭; 其余96个留给读者练习。

你也可以这样使用(c ++):

 int main(){ int x; cin >> x; cout <<(char)x; // In the form of character char a; cin >> a; cout << (int)a; // In the form of `Ascii` code }