0

Need to convert hex dump file to binary data use xxd command (or any other suitable methods that works). The raw hexdump was produced not with xxd. Tried two variants with different options:

xxd -r input.log binout.bin
xxd -r -p input.log binout.bin

Both methods produce wrong results: first command create binary file size 2.2GB, the second command produce binary file size 82382 bytes, both binary file size mismatch, the expected binary size is 65536 bytes.

part of hex file:

807e0000: 4562 537f  e0b1 6477  84bb 6bae  1cfe 81a0 | EbS...dw..k.....
807e0010: 94f9 082b  5870 4868  198f 45fd  8794 de6c | ...+XpHh..E....l
807e0020: b752 7bf8  23ab 73d3  e272 4b02  57e3 1f8f | .R{.#.s..rK.W...
807e0030: 2a66 55ab  07b2 eb28  032f b5c2  9a86 c57b | *fU....(./.....{
807e0040: a5d3 3708  f230 2887  b223 bfa5  ba02 036a | ..7..0(..#.....j
807e0050: 5ced 1682  2b8a cf1c  92a7 79b4  f0f3 07f2 | \...+.....y.....
807e0060: a14e 69e2  cd65 daf4  d506 05be  1fd1 3462 | .Ni..e........4b

What can be the issue here and how to convert data correctly?

1 Answer 1

2

After the xxd you need to remove the first and last parts.

$ sed -i 's/^\(.\)\{9\}//g' binary.txt
$ sed -i 's/\(.\)\{16\}$//g' binary.txt

binary.txt is the name of your file after xxd.

After that you can convert it to binary again.

$ for i in $(cat binary.txt) ; do printf "\x$i" ; done > mybinary

After this if you have the original .bin file you can check md5sums of the files to see if they have the same value. If they have same value then the transformation completed succesfully.

$ md5sum originbinary 
$ md5sum mybinary

You can cover more details in the first part of this link. https://acassis.wordpress.com/2012/10/21/how-to-transfer-files-to-a-linux-embedded-system-over-serial/

6
  • that's a good point. So I should first parse hex file and remove line numbers and ASCII column. In my case the ascii column width, seems, 19 chars (count all chars including spaces).
    – user4515590
    Commented Jul 17, 2018 at 19:26
  • If revert modified plain style hexdump with xxd -r -p, the output binary file have correct size, if revert with xxd -r option, the output binary file is larger by one byte. Why?
    – user4515590
    Commented Jul 17, 2018 at 19:55
  • Can you comment your bash command? Got error: bash: printf: missing hex digit for \x.
    – user4515590
    Commented Jul 17, 2018 at 20:09
  • Which steps are you following ? If you already have a hexdump file as you mentioned earlier you dont need to do 'xxd -r'. You need to do the for loop only then check the md5sums. If you do 'xxd -r' then you don't need to do the for loop because then you will be trying to convert a binary file to binary and you'll get errors. Try only for loop with hexdump file. ( Don't forget to do the sed part)
    – Ugur Akgul
    Commented Jul 18, 2018 at 7:20
  • I already converted to binary with xxd -r -p. The question was why size differes if use command without -p. My last comment relates to another case, when I tried to convert cleaned hexdump use your command.
    – user4515590
    Commented Jul 18, 2018 at 7:59