ASCII(美国信息交换标准代码)表中有128个字符,值的范围从0到127。
一些不同字符的ASCII值如下-
| 字符 | ASCII值 |
|---|---|
| A | 65 |
| a | 97 |
| Z | 90 |
| z | 122 |
| $ | 36 |
| & | 38 |
| ? | 63 |
查找字符的ASCII值的程序如下所示-
#include <iostream>
using namespace std;
void printASCII(char c) {
int i = c;
cout<<"The ASCII value of "<<c<<" is "<<i<<endl;
}
int main() {
printASCII('A');
printASCII('a');
printASCII('Z');
printASCII('z');
printASCII('$');
printASCII('&');
printASCII('?');
return 0;
}输出结果
The ASCII value of A is 65 The ASCII value of a is 97 The ASCII value of Z is 90 The ASCII value of z is 122 The ASCII value of $ is 36 The ASCII value of & is 38 The ASCII value of ? is 63
在上面的程序中,该函数printASCII()打印字符的ASCII值。此函数定义一个int变量i,并将字符c的值存储到该变量中。由于i是整数类型,因此字符的相应ASCII代码存储在i中。然后显示c和i的值。
下面的代码段对此进行了演示。
void printASCII(char c) {
int i = c;
cout<<"The ASCII value of "<<c<<" is "<<i<<endl;
}