Search code examples
c++bit-shiftaccent-sensitive

How is é bitwise shifted to a E in C++?


Hello I having trouble understanding this and how for example it take an accent char like é and converts it E9. I could be missing something i get that it bit-shifts right 4. é = 11101000 and E = 01000101 shifting 4 doesn't makeE right?

static const char *digits = "0123456789ABCDEF";
unsigned char ch;
*dest++ = digits[(ch >> 4) & 0x0F];//this returns E
*dest++ = digits[ch & 0x0F];//this returns 9

Solution

  • The code doesn't convert é to E9 - it converts an 8-bit number to its hexadecimal representation, in four-bit pieces ("nybbles").

    digits[(ch >> 4) & 0x0F] is the digit that represents the high nybble, and digits[ch & 0x0F] is the digit that represents the low nybble.

    If you see é become E9, it's because é has the value 233 in your character encoding.