Is there any logic behind ASCII codes' ordering?

前端 未结 7 2009
星月不相逢
星月不相逢 2020-12-23 19:56

I was teaching C to my younger brother studying engineering. I was explaining him how different data-types are actually stored in the memory. I explained him the logistics b

7条回答
  •  清酒与你
    2020-12-23 20:11

    There are historical reasons, mainly to make ASCII codes easy to convert:

    Digits (0x30 to 0x39) have the binary prefix 110000:

    0 is 110000
    1 is 110001
    2 is 110010
    

    etc. So if you wipe out the prefix (the first two '1's), you end up with the digit in binary coded decimal.

    Capital letters have the binary prefix 1000000:

    A is 1000001
    B is 1000010
    C is 1000011
    

    etc. Same thing, if you remove the prefix (the first '1'), you end up with alphabet-indexed characters (A is 1, Z is 26, etc).

    Lowercase letters have the binary prefix 1100000:

    a is 1100001
    b is 1100010
    c is 1100011
    

    etc. Same as above. So if you add 32 (100000) to a capital letter, you have the lowercase version.

提交回复
热议问题