If your first byte is an ASCII character (as indicated by your example) and your second byte is '\x00', then you probably have data encoded as UTF-16LE.
However it would be a good idea if you showed us unequivocably exactly what's in the first few bytes of your file. Please do this:
python -c "print(repr(open('myfile.txt', 'rb').read(20)))"
and edit your question to show us the result. If any text is confidential, please retain the sense when editing it.
We are especially interested to see if it starts with a UTF-16 BOM ('\xff\xfe'
or '\xfe\xff'
).
For background, what platform (Windows or Linux) are you on? What produced the file?
Update I'm a bit puzzled by your statement """I tried '40s' and 's' but it shows weird data, or only unpacks 1 character instead of 40.""" Examine the following examples:
>>> data = "q\x00w\x00"
>>> unpack("4s", data)
('q\x00w\x00',) # weird? it's effectively tuple([data])
>>> unpack("s", data)
# doesn't produce a string of length 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: unpack requires a string argument of length 1
>>> unpack("ssss", data)
('q', '\x00', 'w', '\x00') # this == tuple(data)
>>>
@pxh commented """You're only getting a single character because those dots are being read as ASCII NULs (and so terminating the string).""" I doubt very much whether @pxh could actually demonstrate that struct.unpack's use of the "s"
format depends in any way on the individual byte values in the data, whether NUL
("\x00"
) or anything else.