c++ - Difference between converting int to char by (char) and by ASCII -
i have example:
int var = 5; char ch = (char)var; char ch2 = var+48; cout << ch << endl; cout << ch2 << endl; i had other code. (char) returned wrong answer, +48 didn't. when changed only (char) +48, code got corrected.
what difference between converting int char using (char) , +48 (ascii) in c++?
char ch=(char)var; has same effect char ch=var; , assigns numeric value 5 ch. you're using ascii (like modern systems) , ascii character code 5 represents enquiry 'enq' old terminal control code. perhaps old timer has clue did!
char ch2 = var+48; assigns numeric value 53 ch2 happens represent ascii character digit '5'. ascii 48 0 (0) , digits appear in ascii table in order after that. 48+5 lands on 53 (which represents character '5').
in c++ char integer type. value interpreted representing ascii character should thought of holding number.
its numeric range either [-128,127] or [0,255]. that's because c++ requires sizeof(char)==1 , modern platforms have 8 bit bytes.
nb: c++ doesn't mandate ascii, again case on modern platforms.
ps: think unfortunate artifact of c (inherited c++) sizeof(char)==1 , there isn't separate fundamental type called byte.
Comments
Post a Comment