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...??