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

C++ Program: ASCII Lookup Table (Octal, Decimal, Hex, HTML)

ASCII Lookup Table: แสดงค่า Octal, Decimal, Hexadecimal, HTML ของตัวอักษร

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

#include <iostream>
#include <string>
using namespace std;

int toOctal(int decimal) {
    int octal = 0, place = 1;
    while (decimal > 0) {
        octal += (decimal % 8) * place;
        decimal /= 8;
        place *= 10;
    }
    return octal;
}

string toHex(int decimal) {
    string hex = "";
    while (decimal > 0) {
        int rem = decimal % 16;
        if (rem < 10) hex = char('0' + rem) + hex;
        else hex = char('A' + rem - 10) + hex;
        decimal /= 16;
    }
    return hex.empty() ? "0" : hex;
}

string toHTML(int decimal) {
    return "&#" + to_string(decimal) + ";";
}

void printASCII(char ch) {
    int dec = (int)ch;
    cout << "Character: '" << ch << "'" << endl;
    cout << "Octal: " << toOctal(dec) << endl;
    cout << "Decimal: " << dec << endl;
    cout << "Hexadecimal: " << toHex(dec) << endl;
    cout << "HTML: " << toHTML(dec) << endl;
}

int main() {
    char ch = 'a';
    printASCII(ch);
    return 0;
}

ผลลัพธ์

Character: 'a'
Octal: 141
Decimal: 97
Hexadecimal: 61
HTML: a

ตาราง ASCII พื้นฐาน

CharDecHexOct
Space322040
0-948-5730-3960-71
A-Z65-9041-5A101-132
a-z97-12261-7A141-172

ฟังก์ชันในตัว C++

// ใช้ iomanip
cout << oct << 97 << endl;  // 141
cout << hex << 97 << endl;  // 61
cout << dec << 97 << endl;  // 97