Code:
""" I wrote this script to illustrate the problem:
'\x1a' hex string gets truncated when read from file
Does anyone know a workaround?
"""
import struct
x = struct.pack('chhh','a', 6668,22,33)
# x is now 'a\x00\x0c\x1a\x16\x00!\x00'
# just to prove that it is working:
print "\noriginal data:\n"+`x`
print "\n\nunpacking original Data is no problem:\n"+ `struct.unpack('chhh',x)`
#now comes the interesting part...
#when
f = open('test.tmp','w')
f.write(x)
f.close()
f = open('test.tmp','r')
newx = f.read()
try:
print struct.unpack('chhh',newx)
except:
print "\n\nBut when I write and retrieve data from file, should be:\n"+`x`+"\n\nbut it's been truncated at \\x1a. See:\n"+`newx`