我们知道,每个字符都有自己的 ASCII 值。在这个 C++ 程序中,我们打印所有字符的 ASCII 值。在这个 C++ 代码中,我们使用 for 循环 (for(i = 0; i <= 255; i++)) 来迭代从 0 到 255 的值。在其中,每个数字都被转换为字符((char)i)。
#include<iostream>
using namespace std;
int main()
{
int i;
cout << "\nThe ASCII Values of all the Characters are\n";
for(i = 0; i <= 255; i++)
{
cout << "The ASCII value of " << (char)i << " = " << i << endl;
}
return 0;
}

C++ 程序使用 While 循环打印所有字符的 ASCII 值
#include<iostream>
using namespace std;
int main()
{
int i = 0;
cout << "\nThe ASCII Values of all the Characters are\n";
while(i <= 255)
{
cout << "The ASCII value of " << (char)i << " = " << i << endl;
i++;
}
return 0;
}
....
The ASCII value of O = 79
The ASCII value of P = 80
The ASCII value of Q = 81
The ASCII value of R = 82
The ASCII value of S = 83
The ASCII value of T = 84
The ASCII value of U = 85
The ASCII value of V = 86
The ASCII value of W = 87
....
这个 程序 打印所有字符的 ASCII 值示例允许用户输入起始和结束 ASCII 值。接下来,它打印这些数字之间所有字符的 ASCII 值。
#include<iostream>
using namespace std;
int main()
{
int i, start, end;
cout << "\nPlease enter the Starting ASCII Value = ";
cin >> start;
cout << "\nPlease enter the Ending ASCII Value = ";
cin >> end;
cout << "\nThe ASCII Values of Characters between " << start << " and " << end << " are\n";
for(i = start; i <= end; i++)
{
cout << "The ASCII value of " << (char)i << " = " << i << endl;
}
return 0;
}
Please enter the Starting ASCII Value = 45
Please enter the Ending ASCII Value = 65
The ASCII Values of Characters between 45 and 65 are
The ASCII value of - = 45
The ASCII value of . = 46
The ASCII value of / = 47
The ASCII value of 0 = 48
The ASCII value of 1 = 49
The ASCII value of 2 = 50
The ASCII value of 3 = 51
The ASCII value of 4 = 52
The ASCII value of 5 = 53
The ASCII value of 6 = 54
The ASCII value of 7 = 55
The ASCII value of 8 = 56
The ASCII value of 9 = 57
The ASCII value of : = 58
The ASCII value of ; = 59
The ASCII value of < = 60
The ASCII value of = = 61
The ASCII value of > = 62
The ASCII value of ? = 63
The ASCII value of @ = 64
The ASCII value of A = 65