Search code examples
pythonbinascii

Python binascii.unhexlify is converting linefeed characters. Can I prevent this?


I have an string of pairs of characters representing hex numbers, (e.g. 0f means decimal 15). I need this in binary format, so I called binArray = binascii.unhexlify(finalString)

I have "bash on Ubuntu on Windows" installed, and when I perform this from there it works fine.

However, I need to invoke the script from a .bat file in the windows environment. When I call the exact same python program from the .bat file, if unhexlify sees '0a' in the hex string it converts it to 0d 0a (in the binary output). This is not a text file and the line-end conversion is corrupting my data!

Is there a way to convince unhexlify to stop doing this when I call it from the windows environment?

It would seem like a waste to have to roll-my-own conversion.


Solution

  • I found out why the 0x0A characters were getting converted to 0D0A (I.E. LF to CRLF)

    It's not the fault of binascii. I used

    outfile = open(scriptDir + "Output/NVRAM/NVDATAout", 'w') to open the file, which implies text mode, and therefore writes get linefeed conversion appropriate for the local system.

    When I opened the output file in binary mode, this conversion stopped happening.

    outfile = open(scriptDir + "Output/NVRAM/NVDATAout", 'wb')

    I found this answer here: https://stackoverflow.com/a/9184137/7275012