简体   繁体   中英

Python writing incorrect hex values

While attempting to write a hex value to ascii (\xdd\xba\x01\x10), I found that two of the values were being changed (\xdd\xba).

Instead of outputting ݺ or \xdd\xba\x01\x10 The output would be ݺ \x77a\x01\x10

I thought the issue might have been just how it was being assigned to the test var but using pack does the same.

test1 = "\xdd\xba\x01\x10" 
test2 = struct.pack("<I", 0x1001badd)

Both result in incorrect hex values being written.

I have temporarily fixed it by adding whitespace and cleaning it up when it writes to a file.

test1 = "\xdd \xba\x01\x10" 
cleanup = (test1)
file.write(cleanup.replace(" ", ""))

Python version is 2.7.2 OS is WINXP

and no I can't use Python3 or a newer version of Windows

Does anyone have any idea why this is occurring?

reverse the values, the program is reading d as a unhexifying string you have to reverse the hex to fix this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM