ตาราง ASCII (American Standard Code for Information Interchange) มีอักขระ 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;
}