Use the built-in function chr() to convert the number to character, then encode that:

>>> chr(int('fd9b', 16)).encode('utf-8')
'\xef\xb6\x9b'

This is the string itself. If you want the string as ASCII hex, you'd need to walk through and convert each character c to hex, using hex(ord(c)) or similar.

Note: If you are still stuck with Python 2, you can use unichr() instead.

Answer from unwind on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
UTF stands for “Unicode Transformation Format”, and the ‘8’ means that 8-bit values are used in the encoding. (There are also UTF-16 and UTF-32 encodings, but they are less frequently used than UTF-8.) UTF-8 uses the following rules: If the code point is < 128, it’s represented by the corresponding byte value.
🌐
Python Cheat Sheet
pythonsheets.com › notes › basic › python-unicode.html
Unicode — Python Cheat Sheet
A Unicode code point transfers to a byte string is called encoding. >>> s = u'Café' >>> type(s.encode('utf-8')) <class 'bytes'>
🌐
Stack Overflow
stackoverflow.com › questions › 47631594 › unicode-code-points-to-utf-8-python
unicode code points to utf-8 python - Stack Overflow
December 4, 2017 - Copy# -*- coding: utf-8 -*- string1 = "آکاش کمار" string2 = string1.decode('utf-8') string3 = string2.encode('utf-8') print(string3) ... Sign up to request clarification or add additional context in comments. ... The tweet data in the image looks nothing like what you assume. 2017-12-04T11:20:21.647Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... You don’t understand DNS like you think you... Dispatches from O'Reilly: From capabilities to... ... 1519 UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - This means that you don’t need # -*- coding: UTF-8 -*- at the top of .py files in Python 3. All text (str) is Unicode by default. Encoded Unicode text is represented as binary data (bytes).
Find elsewhere
🌐
Python
docs.python.org › 3.0 › howto › unicode.html
Unicode HOWTO — Python v3.0.1 documentation
UTF stands for “Unicode Transformation Format”, and the ‘8’ means that 8-bit numbers are used in the encoding. (There’s also a UTF-16 encoding, but it’s less frequently used than UTF-8.) UTF-8 uses the following rules: If the code point is <128, it’s represented by the corresponding byte value.
Top answer
1 of 3
17

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
2 of 3
13

You can only map an "integer number" from one encoding to another if they are both single-byte encodings.

Here's an example using "iso-8859-15" and "cp1252" (aka "ANSI"):

>>> s = u'€'
>>> s.encode('iso-8859-15')
'\xa4'
>>> s.encode('cp1252')
'\x80'
>>> ord(s.encode('cp1252'))
128
>>> ord(s.encode('iso-8859-15'))
164

Note that ord is here being used to get the ordinal number of the encoded byte. Using ord on the original unicode string would give its unicode code point:

>>> ord(s)
8364

The reverse operation to ord can be done using either chr (for codes in the range 0 to 127) or unichr (for codes in the range 0 to sys.maxunicode):

>>> print chr(65)
A
>>> print unichr(8364)
€

For multi-byte encodings, a simple "integer number" mapping is usually not possible.

Here's the same example as above, but using "iso-8859-15" and "utf-8":

>>> s = u'€'
>>> s.encode('iso-8859-15')
'\xa4'
>>> s.encode('utf-8')
'\xe2\x82\xac'
>>> [ord(c) for c in s.encode('iso-8859-15')]
[164]
>>> [ord(c) for c in s.encode('utf-8')]
[226, 130, 172]

The "utf-8" encoding uses three bytes to encode the same character, so a one-to-one mapping is not possible. Having said that, many encodings (including "utf-8") are designed to be ASCII-compatible, so a mapping is usually possible for codes in the range 0-127 (but only trivially so, because the code will always be the same).

🌐
Python GTK+ 3 Tutorial
python-gtk-3-tutorial.readthedocs.io › en › latest › unicode.html
4. How to Deal With Strings — Python GTK+ 3 Tutorial 3.4 documentation
If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.) Although ASCII encoding is simple to apply it can only encode for 128 different characters which is hardly enough. One of the most commonly used encodings that addresses this problem is UTF-8 (it can handle any Unicode code point).
🌐
freeCodeCamp
freecodecamp.org › news › a-beginner-friendly-guide-to-unicode-d6d45a903515
A Beginner-Friendly Guide to Unicode in Python
July 18, 2018 - Given a code point, UTF-8 encodes it into a sequence of bytes. Given a sequence of bytes, UTF-8 decodes it into a code point. The correct rendering of Unicode characters involves traversing a chain, ranging from bytes to code points to glyphs.
🌐
Online Tools
onlinetools.com › utf8 › convert-code-points-to-utf8
Convert Code Points to UTF8 – Online UTF8 Tools
The Unicode standard defines code ... construct UTF8-encoded characters from the input code points, you must specify the numeric base of the code points....
🌐
GitHub
github.com › maximilianh › maxtools › blob › master › lib › unicodeConvert.py
maxtools/lib/unicodeConvert.py at master · maximilianh/maxtools
UTF-8: "E4 B8 80" [used in the "UTF-8" field in Unihan.txt] · ------------------------------------------------------------------------------- · Unihan UCN: "U+4E00" [used by Unicode Inc.] · Unihan NCR ...
Author: maximilianh