Unicode Code Point to ASCII Character Conversion

  • Share this:

Code introduction


Converts a given Unicode code point to its corresponding ASCII character. If the code point cannot be directly converted to an ASCII character, it returns a question mark.


Technology Stack : String, encoding

Code Type : String processing

Code Difficulty : Beginner


                
                    
def achr(codepoint, encoding='utf-8'):
    """
    将Unicode码点转换为对应的ASCII字符。
    """
    try:
        return bytes([codepoint]).decode(encoding)
    except UnicodeDecodeError:
        return '?'