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 พื้นฐาน
| Char | Dec | Hex | Oct |
|---|---|---|---|
| Space | 32 | 20 | 40 |
| 0-9 | 48-57 | 30-39 | 60-71 |
| A-Z | 65-90 | 41-5A | 101-132 |
| a-z | 97-122 | 61-7A | 141-172 |
ฟังก์ชันในตัว C++
// ใช้ iomanip cout << oct << 97 << endl; // 141 cout << hex << 97 << endl; // 61 cout << dec << 97 << endl; // 97