How to convert python byte string containing a mix of hex characters? -


specifically, receiving stream of bytes tcp socket looks this:

inc_tcp_data = b'\x02hello\x1cthisisthedata' 

the stream using hex values denote different parts of incoming data. want use inc_data in following format:

converted_data = '\x02hello\x1cthisisthedata' 

essentially want rid of b , literally spit out came in.

i've tried various struct.unpack methods .decode("encoding). not former work @ all, , latter strip out hex values if there no visual way encode or convert character if could. ideas?

update:

i able desired result following code:

inc_tcp_data = b'\x02hello\x3fthisisthedata'.decode("ascii")   d = repr(inc_tcp_data)  print(d) print(len(d)) print(len(inc_tcp_data)) 

the output is:

'\x02hello?thisisthedata' 25 20 

however, still doesn't me because need regular expression follows see \x02 hex value , not 4 byte string.

what doing wrong?

update

i've solved issue not solving it. reason wanted hex characters remain unchanged regular expression able detect further down road. should have done (and did) change regular expression analyze bytes without decoding it. once had separated out parts via regular expression, decoded parts .decode("ascii") , worked out great.

i'm updating if happens else.

assuming using python 3

>>> inc_tcp_data.decode('ascii') '\x02hello\x1cthisisthedata' 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -