Negative ASCII value

前端 未结 6 1402
太阳男子
太阳男子 2020-12-09 19:33

What\'s the point of negative ASCII values?

int a = \'«\'; //a = -85 but as in ASCII table \'<<\' should be 174
6条回答
  •  醉酒成梦
    2020-12-09 19:52

    There are no negative ASCII values. ASCII includes definitions for 128 characters. Their indexes are all positive (or zero!).

    You're seeing this negative value because the character is from an Extended ASCII set and is too large to fit into the char literal. The value therefore overflows into the bit of your char (signed on your system, apparently) that defines negativeness.

    The workaround is to write the value directly:

    unsigned char a = 0xAE; // «
    

    I've written it in hexadecimal notation for convention and because I think it looks prettier than 174. :)

提交回复
热议问题