I have a bunch of binary data that comes to python via a char* from some C interface (not under my control) so I have a string of arbitrary binary data (what is normally a byte array). I would like to convert it to a byte array to simplify using it with other python functions but I can't seem to figure out how.
Examples that don't work:
data = rawdatastr.encode()
this assumes "utf-8" and mangles the data == BAD
data = rawdatastr.encode('ascii','ignore')
strips chars over 127 == BAD
data = rawdatastr.encode('latin1')
not sure -- this is the closest so far but I have no proof that it is working for all bytes.
data = array.array('B', [x for x in map(ord,data)]).tobytes()
This works but seems like a lot of work to do something simple. Is there something simpler?
I am thinking I need to write my own identity encoding that just passes the bytes along (I think latin1 does this based upon some reading but no proof thus far).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…