Command for converting between ASCII text files and binary files

时间:2022-02-22 04:48:27

1.

While wanting to convert binary files(especially for an embedded system image file) into ASCII text files,  linux command "od" could be used.

such as the usage,

                   [shawn]$ od -A n -t x4 -w4 -v    456.bin > 123.txt

In the command above,

               -A n :  DO NOT print file offsets

               -t x4:  output format - 4 bytes per integer

               -w4 : output 4 bytes per output line

               -v :  do not use * to mark line suppression

part of the result text file after converting is as below,

 000005d8
 3c1d9e80     <--- one MIPS instruction

2.

When wanna convert text files(mainly for the text file from binary image file) into binary files,  linux command "xxd" can be used.

such as,

                          [shawn]$ xxd -r -p 123.txt > 456.bin

In the command above,

-r | -revert
              reverse operation: convert (or patch) hexdump into binary.  If  not
              writing to stdout, xxd writes into its output file without truncat-
              ing it. Use the combination -r -p to read plain  hexadecimal  dumps
              without  line  number  information  and without a particular column
              layout. Additional Whitespace and line-breaks are allowed anywhere.

The commands mentioned above are used in our embedded project, please feel free to use them to reach your goal.