Reading 40 bytes of binary data as ascii text

I have some binary data, in a hex editor it looks like this: somedata

with all these dots between each letter

when i read filehandle.read (40) it shows these points

I know that dots should not be there, is there a way to unpack some 40-byte ascii data with a structure?

I tried "40" and "but", but it shows strange data or only unpacks 1 character instead of 40.

+3
source share
3 answers

If your first byte is an ASCII character (as shown in the example) and your second byte is '\ x00', then you probably have data encoded as UTF-16LE.

, , . , :

python -c "print(repr(open('myfile.txt', 'rb').read(20)))"

, . - , .

, UTF-16 ('\xff\xfe' '\xfe\xff').

, (Windows Linux) ? ?

" " 40 "" ", 1 40. "" :

>>> data = "q\x00w\x00"
>>> unpack("4s", data)
('q\x00w\x00',) # weird? it 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 "" , NULS ASCII (, , ). "" " , @pxh . "s" , NUL ("\x00") - .

+4

s[::2], s - 80 , . " : @fadden" UTF-16 ( .encode ASCII ..), Q & D , ( , 256, Q & D , - ...).

+1

python :

val = f.read(1)
val = struct.unpack( 'c' , val )

, . 40-

val = f.read(40)
val = struct.unpack( '40c' , val )
0

Source: https://habr.com/ru/post/1760210/


All Articles