I need to convert python value into byte array and vice versa.
For example:
- Integer 256 -> [1, 1] // 256 = 1 * 255 + 1
- String 'ab' -> [97, 98] // 'a' -> 97, 'b' -> 98
- Floating point 1.23 -> [63, 157, 112, 164] // 1.23 as 4 byte representation
For java kryo can be used for this purpose: byte[] serializedValue = kryoSerializer.writeObjectData(value);
gives me the serialized result of value.
I tried pickle, but I can't use it as it consumes 6 bytes for storing an integer object.
import pickle
Foo = 256
picklestring = pickle.dumps(Foo)
print len(picklestring) # returns 6
Any hints for this?
ADDED
# http://docs.python.org/2/library/struct.html
# http://stackoverflow.com/questions/16818463/python-encode-decoder-for-serialization-deserialization-javas-kyro-equivalence
# http://stackoverflow.com/questions/11624190/python-convert-string-to-byte-array
import struct
# >f for
def encode(value):
formatString = ""
if type(value) is float:
formatString = ">f"
elif type(value) is int:
formatString = ">i"
elif type(value) is str:
formatString = ">s"
else:
raise Exception("Wrong data input: only supports float/int/string")
packed = struct.pack(formatString, value)
result = []
for i in packed:
# i is a string
result.append(ord(i[0]))
return result