Cloud
Cloud

Reputation: 19331

BASh - Convert list of hex values to binary file (application)

I have a file containing hex representations of code from a small program, and am trying to actually convert it into the program itself.

For example, here is a sample of such text, stored in a file, input.txt:

8d
00
a1
21
53
57
43
48
0e
00
bb

I am using the following BASh snippet to convert the file to a binary file:

rm outfile; while read h; do echo -n ${h}; echo -ne \\x${h} >> outfile; done < input.txt

After opening the output file in VIM:

¡!SWCH»

And then converting it to hex representation via xxd:

0000000: 8d00 a121 5357 4348 0e00 bb0a            ...!SWCH....

This is all good, except for one thing: There is a trailing byte, 0a, trailing at the end of my binary output file. This happens for every program file I work with. How is the trailing 0a being appending to every output binary file? It's not present in my input file.

Thank you.

Upvotes: 1

Views: 3023

Answers (1)

clt60
clt60

Reputation: 63974

Simply, use xxd directly from a bash like

xxd outfile > outfile.hex

and you will see, here isn't any 0a.

The 0a is appended somewhere when the vim sends a line to xxd command. If you want convert inside vim - try use

vim -b outfile

what open the outfile in binary mode.

Upvotes: 2

Related Questions