-1

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
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
k.wig
  • 39
  • 1
  • 7
  • 2
    Explain, please, what do you want to achieve – mcsim Aug 16 '18 at 13:49
  • Binary `11101000` shifted left by 4 bits is binary `1110`, or decimal 14. The string `digits` happens to have a character `E` at index 14. – Igor Tandetnik Aug 16 '18 at 13:52
  • 2
    You seem to be confusing ascii-representations with hex values. `digits[('é'>>4)&0x0F]` is simply the 14th value in your `digits` array, which is `E`. It has nothing to do with the ascii-representation of `"E"`. – Zinki Aug 16 '18 at 13:53
  • @IgorTandetnik thanks – k.wig Aug 16 '18 at 13:56
  • `é = 11101000` Note that this is only true in your specific character encoding. It is not true in UTF-8 or any other Unicode encoding. – sepp2k Aug 16 '18 at 14:20

1 Answers1

0

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82