0

I want to know is there any linux utility or procedure for generating plain binary dump and reversing it back to the original file (as xxd is capable of doing it with -r argument)? I have tried xxd, it have the option to reverse to original file but it only prints plain hex dumps.

In case if above is not possible, is there any utility for conversion of plain hex strings (Example : hx829jdxkdiei) into plain binary. On windows there are some programs like 010 Editor, but I am not able to find any for Command line linux.

5
  • 1
    generating plain binary dump and reversing it back ... back to what? ... it is unclear what you are trying to do
    – jsotola
    Sep 12 at 4:33
  • @jsotola As xxd is capable of doing it with -r argument
    – Akash
    Sep 12 at 4:47
  • hx829jdxkdiei can't be hex. Did you mean base64?
    – muru
    Sep 12 at 5:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 20 at 0:52
3

If you just want to dump to hex, I would use hexdump, or xxd.

However, you mentioned 010 Editor, which is a hex editor. There are plenty of hex editors for Linux. For example, see this part of the Arch wiki, or this Wikipedia page.

1

You can always use perl:

perl -ple 'BEGIN{$/=\8} $_=unpack("B*")' < file > file.dump

(B for the 0x1 8-bit byte (octet) to be dumped as 00000001; replace with b to get 10000000 instead)

Reverse:

perl -pe '$_=pack("B*", $_)' < file.dump > file

Example:

$ printf %s hx829jdxkdiei | perl -ple 'BEGIN{$/=\8}$_=unpack("B*")'
0110100001111000001110000011001000111001011010100110010001111000
0110101101100100011010010110010101101001
$ printf %s hx829jdxkdiei | perl -ple 'BEGIN{$/=\8}$_=unpack("B*")' |
    perl -pe '$_=pack("B*", $_)'
hx829jdxkdiei

For hex dump to binary dump:

$ echo ff0aa0ff | perl -ple '$_=unpack("B*", pack("H*",$_))'
11111111000010101010000011111111
$ printf %s hx829jdxkdiei | xxd -p -c8 | perl -ple '$_=unpack("B*", pack("H*",$_))'
0110100001111000001110000011001000111001011010100110010001111000
0110101101100100011010010110010101101001

With zsh, see also:

$ string=hx829jdxkdiei
$ set +o multibyte -o extendedglob
$ bin=${string//(#m)?/${(l[8][0])$(( [##2] ##$MATCH ))}}
$ print -r -- $bin
01101000011110000011100000110010001110010110101001100100011110000110101101100100011010010110010101101001
$ print ${bin//(#m)?(#c8)/${(#):-"2#$MATCH"}}
hx829jdxkdiei

Where

  • $(( ##x )) gives you the value of character x (wchar_t value with the multibyte option on, byte value otherwise).
  • $(( [##2] expression )) expands the result of the arithmetic expression in binary (without the 2# prefix).
  • ${(l[8][0])param} left pads the parameter expansion to length 8 with 0s.
  • ${(#)param} outputs the character whose value is stored in $param (reverse of $(( ##x ))).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.