C/C++: Converting hexadecimal value in char to integer

前端 未结 7 1637
萌比男神i
萌比男神i 2021-01-01 06:28

I have hexadecimal values stored as characters:

char A = \'0\';
char B = \'6\';
char C = \'E\';

... I need them coverted to integers. I kno

7条回答
  •  暖寄归人
    2021-01-01 06:30

    int v = (A > '9')? (A &~ 0x20) - 'A' + 10: (A - '0');
    

    is correct for ASCII. For other character sets, a similar approach would work, but you would then want toupper instead of &~ 0x20.

提交回复
热议问题