Parse text file with addresses, byte values, and comments to binary file
0
votes
2
answers
518
views
I'm attempting to start with a file in the following form:
~~~
00: 42 ; byte 0 is 0x42 / d
01: 52 ; byte 1 is 0x52 / r
02: 62 ; byte 2 is 0x62 / D
03: 72 ; byte 3 is 0x72 / R
07: 1f ; bytes 03..0e are implicitly 00, byte 0f is 0x1f
~~~
and use it to generate an 8-byte file whose values (in hex form) would be:
42 52 62 72 00 00 00 1f
The exact input format isn't carved in stone. I just picked ';' as a comment delimiter because it's a single character and unambiguous. The form of the offset: and 2-digit hex value just seemed obvious based on tradition.
I suspect the ultimate solution involves using sed
or awk
to strip away the comment, then piping their output to xxd
, but so far my first experiment has fallen flat on its face & I can't even get xxd to parse what ought to be a best-case simple text file.
---
For my first attempt, I simplified config.src:
~~~
00: 42
01: 52
02: 62
03: 72
~~~
(omitting the comments and implied zero-bytes for now, and sticking to values corresponding to printable ASCII)
... then tried to generate the binary file from it:
xxd -r config.src config.bin
What I expected to see from cat config.bin
and xxd config.bin
:
BRbr
and 00000000: 42 52 62 72 BRbr
What I ended up with:
a 2-byte file with unprintable content cat
can't render, and the following output from xxd config.bin
: 00000000: 0301
So... problem #1... *What am I doing wrong with xxd*, and how can I fix it (or is there a better approach)? Keep in mind that I really want to specify one byte value per line, and would really like to be able to automatically skip sequential values and have them automatically filled with zeroes.
The... problem #2... once I get xxd to parse my file, how can I go add the comments and strip them away before xxd sees them?
Note that I'm not hellbent on using xxd per se... but this is a shared web server to which I don't have root or admin access, so apt-get install
isn't an option, and compiling my own copies from source wouldn't necessarily be easy).
---
*(Background info... not necessary essential to solving the problem, but adding context to why I'm trying to do it)*
I'm working on an Arduino-based IoT controller. For the past few weeks, its configuration has consisted of hardcoded values and various interpretations of a DIP switch I've repurposed every few days. It's getting tedious. I'm not in the mood yet to implement a proper UI, so I came up with the idea of having it just fetch a binary config blob from my web server into a char[] as its first act upon starting up (enabling me to tweak runtime config values without having to go all the way and reflash the board itself, which is honestly kind of a pain at this point).
Asked by Bitbang3r
(123 rep)
Apr 27, 2020, 12:39 AM
Last activity: Apr 27, 2020, 02:23 PM
Last activity: Apr 27, 2020, 02:23 PM