C++程式:查詢字元的ASCII值


ASCII(美國資訊交換標準程式碼)表中有128個字元,其值範圍為0到127。

一些不同字元的ASCII值如下:

字元ASCII值
A65
a97
Z90
z122
$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;
}

更新於:2020年6月24日

10K+瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.