1

I'm using socket, struct for receive and unpack the received bytes message via tcp/ip protocol, I'm getting tuple which contains numeric data as well as bytes in the defined order as per contract. The example data as below...

Example: receive buffer data from tcp ip

buffer = sock.recv(61)

Unpack the bytes into predefined struct format

tup_data = struct.unpack("<lll8s17sh22s", buffer)

tup_data
(61,12000,4000,b'msg\x00\x00\x00\x00\x00',b'anther 
msg\x00\x00\x00\x00\x00\x00\x00',4,b'yet another 
msg\x00\x00\x00\x00\x00\x00\x00')

since the data is highly streaming and execution time is matter... I don't want to load the cpu by using any looping and isinstance() method. Since the location of bytes are defined, so I'm currently using as

processed_data = (*tup_data[:3],
                   tup_data[3].strip(b"\x00").decode(),
                   tup_data[4].strip(b"\x00").decode(),
                   tup_data[5],
                   tup_data[6].strip(b"\x00").decode())

processed_data
(61,12000,4000,"msg","anther msg",4,"yet another msg")

Is there any magic way to convert bytes into required string at one shot as the location of bytes are known...??

1 Answers1

0

Since you're using struct.unpack for unpacking your buffer and due to the format-characters chart you can't get string format as your output. Therefore you should either strip the extra \x00 at the source or just use a generator comprehension as following to reformat the items that are instances of bytes.

In [12]: tuple(i.strip(b'\x00').decode() if isinstance(i, bytes) else i for i in t)
Out[12]: (61, 12000, 4000, 'msg', 'anther msg', 4, 'yet another msg')
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Will above code still useful in the high frequency data streaming...consider I'm worrying for microseconds ?? – Manjunathan Venkatesan Jun 02 '18 at 09:57
  • @ManjunathanVenkatesan Not that much faster. Also for tuples with small number of items I think your approach is faster but for larger tuples I think this is both more Pythonic and also functional and flexible. – Mazdak Jun 02 '18 at 10:51