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-specific stuff like pickle are off-limits.
What about using the struct module to do your packing/unpacking?
or if you really want just hex characters
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.