How to get character for a given ascii value

后端 未结 9 2682
醉酒成梦
醉酒成梦 2020-11-29 05:51

How can I get the ascii character of a given ascii code.

e.g. I\'m looking for a method that given the code 65 would return \"A\".

Thanks

9条回答
  •  一整个雨季
    2020-11-29 06:15

    Here's a function that works for all 256 bytes, and ensures you'll see a character for each value:

    static char asciiSymbol( byte val )
    {
        if( val < 32 ) return '.';  // Non-printable ASCII
        if( val < 127 ) return (char)val;   // Normal ASCII
        // Workaround the hole in Latin-1 code page
        if( val == 127 ) return '.';
        if( val < 0x90 ) return "€.‚ƒ„…†‡ˆ‰Š‹Œ.Ž."[ val & 0xF ];
        if( val < 0xA0 ) return ".‘’“”•–—˜™š›œ.žŸ"[ val & 0xF ];
        if( val == 0xAD ) return '.';   // Soft hyphen: this symbol is zero-width even in monospace fonts
        return (char)val;   // Normal Latin-1
    }
    

提交回复
热议问题