0

I have an expression like this that produces the list of bytes of the utf-8 representation.

list(chr(number).encode("utf-8"))

But how to do this in reverse?

Say, I have 2 bytes [292, 200] as a list, how can I decode them into a symbol?

3
  • Hello, can you provide one example of a number ? Commented May 9, 2020 at 11:33
  • b.decode('utf-8','replace') Commented May 9, 2020 at 11:34
  • print (list(chr(200).encode("utf-8"))) gives [195, 136] Commented May 9, 2020 at 11:52

2 Answers 2

2

You can call bytes on a list of integers in the range 0..255.

So your example reverses like this:

>>> bytes([195, 136]).decode('utf8')
'È'

If you want the codepoint, wrap it in ord():

>>> ord(bytes([195, 136]).decode('utf8'))
200

Note: the last step only works if the byte sequence corresponds to a single Unicode character (codepoint).

Sign up to request clarification or add additional context in comments.

Comments

1
  1. You have to remember that char only stores 8 bits: -128 to 127. So if 'number' is bigger than char limits it won't work.

    number = 127
    print(f"number: {number}")
    li = list(chr(number).encode("utf-8"))
    print(f"List of byte: {li}")
    dec = int.from_bytes(li, byteorder='big')
    print(f"Type dec: {type(dec)}")
    print(f"Value dec: {dec}")
    

    enter image description here

    number = 128
    print(f"number: {number}")
    li = list(chr(number).encode("utf-8"))
    print(f"List of byte: {li}")
    dec = int.from_bytes(li, byteorder='big')
    print(f"Type dec: {type(dec)}")
    print(f"Value dec: {dec}")
    

    enter image description here

    Take a look at python documentation for converting values

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.