Problem with receiving packets through network

Hey guys,
I have a pretty trivial problem here. I am making a game which uses UDP to send packets through various sockets. It’s working pretty much find and the stuff that I’m sending is being printed in the terminal. However everything that I print has a “b” before it. For example if I send “Hello World” as the message the output to the screen is “b’Hello World’”. Has anyone ever had this problem before? Its proving to be pretty problematic for me.
Thanks in advance,
Adam

This is does not belong to the BGE. Have a look at the Python documentation regarding unicode and strings. There should be some pages regarding this topic.

In any case, if you are making a game, I guess it might belong here too…
Well, to put it simply, the sockets can only send bytes and the byte type is b’’. So to be able to send your data, you must first convert it to bytes. If you print(str(eval(sock.recvfrom(16)[0]))=), you might get you “Hello world” as a string instead of a byte!

You can create a string from bytes using the “.decode” method. If it’s encoded in UTF-8, you don’t need to specify the encoding and can just use “text = message.decode()”

Sorry I meant it different.
You will find a lot of information regarding this topic in the common Python forums as this is not a BGE related thing.

Blender uses Python 3, not Python 2.
In Python 3, strings were changed from ASCII to Unicode. However, many Python libraries still use ASCII text (UTF-8 encoded bytes), which will show up with a b when you print it. See the post of Agoose77 on how to convert bytes to str.

Thanks very much for the replies guys, helped me out completely :slight_smile: