ต่อไปนี้เป็นตัวอย่างการแปลงอักขระเป็น int
ตัวอย่าง
#include <iostream>
using namespace std;
int main() {
char c = '8';
int i = c - 48;
cout << i;
i = c - '0';
cout <<"\t" << i;
return 0;
} ผลลัพธ์
8 8
ในโปรแกรมข้างต้น อักขระ 'c' จะเริ่มต้นด้วยค่า อักขระจะถูกแปลงเป็นค่าจำนวนเต็มดังแสดงด้านล่าง -
char c = '8'; int i = c - 48; cout << i; i = c - '0'; cout <<"\t" << i;