Hex Strings to Binary Files: a show in one unnatural act

Thought I do this up as an action movie…

(Cue opening theme)

This is a snippet of a text file used for configuring an electronic component:


0 = 0x37
1 = 0x0
2 = 0x70
3 = 0x19
4 = 0xc0
5 = 0x7e

0 = 0x0
1 = 0xec
2 = 0xf9
3 = 0x3
4 = 0x8
5 = 0x4

0 = 0x0

0 = 0x10
1 = 0x52
2 = 0xf3
3 = 0x23
4 = 0x1
5 = 0x4

0 = 0x1
1 = 0x2


I wrote some code to parse the file into individual lists plus a final compiled list:

... if Flag==7 and len(line)>1 and line[0]!='[': d0.append(line[-4:]) if Flag==8 and len(line)>1 and line[0]!='[': c5.append(line[-4:]) ...

Each element is a string representing each hex value:
[attachment=0]9-11-2013 2-40-35 PM.png[/attachment]
This was to make things readable so I could easily compare what i was processing to the source file. (In case you were wondering, the 0xFF values are to pad each area to 21 bytes)

At the end, I used this code to put each element into binary and save it to another file used by the actual configuration process:

for x in s: system.file.writeFile(fileOut,chr(int(x,16)),1)

Worked great for a while. Until… (cue dramatic chord)… this value entered the mix:
[attachment=1]9-11-2013 2-40-35 PM markup.png[/attachment]

Reading back the output file gave back a value of 0x3F. Even though my lists were entirely correct! :open_mouth: This kicked my tailfeathers for a few hours (cue car chase music). There was no mathematical or boolean pattern to this. Then something in the back of my brain finally made it up to the front. The line writing the output file is mostly okay, but has a fatal flaw.

chr(), while it does end up with a binary value for the output file, is really only reliable for ASCII-- which only goes up to 0x7F. Anything above that returns a question mark. A question mark is (cue dramatic pause) 0x3F i[/i]

(cue bright music to underscore peppy ending banter)

Changing my earlier appends to:


and changing my file writing to:


completely fixed my issue. While I lose my list readability, I gain robustness, which is really more important anyway.

(freeze on laughing characters)