Search code examples
pythonunicodeencodingutf-8character-codes

Python get character code in different encoding?


Given a character code as integer number in one encoding, how can you get the character code in, say, utf-8 and again as integer?


Solution

  • UTF-8 is a variable-length encoding, so I'll assume you really meant "Unicode code point". Use chr() to convert the character code to a character, decode it, and use ord() to get the code point.

    >>> ord(chr(145).decode('koi8-r'))
    9618