0

I am currently doing project which requires communication from a PC to the device, so far I've decided on socket comms. and have written some code. I am also using ZMQ for ipc on the device itself.

My script works by sending data as text across. I was trying to encode my data to utf-8 so that it can be easily read on the device and displays in a frame and performs the tasks as needed. However, I can't seem to get the encoding working right, I've tried searching for examples or tutorials online but can't seem to find any.

I've tried using socket.send (msg.encode("UTF-8")) to encode my data, and message = socket.recv() to recv & print the data on the server. This works but the server would print out the exact text data which is not what I wanted. I'm unsure whether this is the correct way, and hope that someone could point me in the correct direction for encoding & printing the encoded data without decoding back to text.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
intensified
  • 159
  • 3
  • 4
  • 10

1 Answers1

1

You are receiving the text as encoded UTF8 data. It is all working correctly.

However, if you are printing the data on the receiving end directly to a terminal that happens to be configured to display UTF-8, you won't see any difference.

Print the representation instead:

print repr(message)

to see the string literal representation including any non-printable, non-ASCII bytes displayed as escape strings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Oh, I think get it now. However, even when i print the representation, it still shows the exact same text unless i encode it as hex. I'm not too sure on to print the received data in groups of 2 instead of the entire string of hex lumped together. Also are there any other encodings you would recommend? – intensified Aug 04 '13 at 12:35
  • Also, was wondering how is the data being encoded in UTF8? Is this done by default? – intensified Aug 04 '13 at 14:11
  • You are sending UTF-8 bytes thus receiving UTF-8 bytes. If you want to see only hex, use `binascii.hexlify` – Martijn Pieters Aug 04 '13 at 14:31