C++程序:查找字符的ASCII值
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; }
广告