Luis Peralta Molina
Luis Peralta Molina

Reputation: 11

Convert bytes to string in python

I have the following printed bytes returned from another system using sockets:

b"\x0bMessage Received!\r\x1c\r"

For example: print(b"\x0bMessage Received!\r\x1c\r".decode(encoding="utf-8"))

And a I got

Can you help me to understand how to get an output like this Message Received! from that message.

Upvotes: 1

Views: 390

Answers (2)

snakecharmerb
snakecharmerb

Reputation: 55589

You need to strip out the unwanted characters (in this case vertical tab and carriage return):

>>> bs = b"\x0bMessage Received!\r\x1c\r"
>>> print(bs.decode().strip())
Message Received!

Upvotes: 2

Nayanish Damania
Nayanish Damania

Reputation: 652

b"\x0bMessage Received!\r\x1c\r".decode("utf-8")

Upvotes: -1

Related Questions