Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ หาค่า ASCII ของตัวอักษร


ASCII (American Standard Code for Information Interchange) มี 128 ตัวอักษร ค่า 0-127

ตัวอย่างค่า ASCII:

CharacterASCII Value
A65
a97
Z90
z122
$36
&38
?63

ตัวอย่างโค้ด

#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(char c) กำหนด int i = c เนื่องจาก char เก็บค่าตัวอักษร แต่ int จะเก็บรหัส ASCII:

void printASCII(char c) {
    int i = c;
    cout << "The ASCII value of " << c << " is " << i << endl;
}

main() เรียกพิมพ์หลายตัวอักษร