victor
victor

Reputation: 70

String to utf-8 byte conversion

I am developing a websocket server and I need to send to the client '\x81' encoded in utf-8 and when i do: chr(129).encode('utf-8') it returns b'\xc2\x81'. I expect b\x81. Thank you very much!

ps: i am complete newbie

Finally I have found it. I have used it bytes([129, 21, 34])

Upvotes: 0

Views: 430

Answers (1)

Ahto Truu
Ahto Truu

Reputation: 66

The output you get is correct. UTF-8 can only represent characters up to '\x7f' in a single byte, all characters starting from '\x80' will take up at least two bytes. Check any UTF-8 reference for details. The Wikipedia page is a good start.

Upvotes: 1

Related Questions