I want to encode a set of configuration options into a long string of hex digits.
The input is a mix of numbers (integers and floats) and strings. I can use binascii.a2b_hex
from the standard library for the strings, bit-wise operators for the integers, and probably, if I go and read some on floating point representation (sigh), I can probably handle the floats, too.
Now, my questions:
- When given the list of options, (how) should I type check the value to select the correct conversion routine?
- Isn't there a library function for the numbers, too? I can't seem to find it.
The serialized data is sent to an embedded device and I have limited control over the code that consumes it (meaning, changes are possible, but a hassle). The specification for the serialization seems to conform to C value representation (char arrays for strings, Little Endian integers, IEEE 754 for floats), but it doesn't explicitily state this. So, Python开发者_StackOverflow中文版-specific stuff like pickle are off-limits.
You want struct
.
>>> struct.pack('16sdl', 'Hello, world!', 3.141592654, 42)
'Hello, world!\x00\x00\x00PERT\xfb!\t@*\x00\x00\x00\x00\x00\x00\x00'
You easiest bet is to pickle
the whole list to a string and then use binascii.a2b_hex()
to convert this string to hex digits:
a = ["Hello", 42, 3.1415]
s = binascii.b2a_hex(pickle.dumps(a, 2))
print s
# 80025d710028550548656c6c6f71014b2a47400921cac083126f652e
print pickle.loads(binascii.a2b_hex(s))
# ['Hello', 42, 3.1415]
What about using the struct module to do your packing/unpacking?
import struct
s = struct.pack('S5if',"Hello",42,3.1415)
print s
print struct.unpack('5sif')
or if you really want just hex characters
import struct, binascii
s = binascii.b2a_hex(struct.pack('S5if',"Hello",42,3.1415))
print s
print struct.unpack('5sif',binascii.a2b_hex(s))
Of course this requires that you know the length of strings that are being sent across or you could figure it out by looking for a NULL character or something.
精彩评论