is it possible to reverse xxd to get original file from binary file

13,982

You will have to write your own binary to hex function ...

cut -f2-8 -d' ' infile | tr -d '\n' | tr ' ' '\n' | while read l; 
do 
  echo -n    $(bin_to_hex($l)) >> outfile
  (( ++n )) 
  (( n % 60 == 0)) && echo  "" >> outfile 
done

This should output the same format as -p which can then be run through -r. If you read the man page for xxd you will read that there is no -r for -b. It says so on the excerpt that you included in your question.

Share:
13,982

Related videos on Youtube

arvindh
Author by

arvindh

Updated on September 18, 2022

Comments

  • arvindh
    arvindh almost 2 years

    i have been using xxd to create a hexa representation of a file(any file like .jpg, .docx, .zip etc), like this,..

    $ xxd -p original_file.zip > hexa_format.txt
    

    and to reverse

    $ xxd -r -p hexa_format.txt > original_file.zip
    

    if anybody feels that this isn't the proper way, please correct me. anyhow this isn't my question. now i have to convert them to binary instead of hexa. so the command goes like this, if im correct.

    $ xxd -p -b original_file.zip > binary_format.txt
    

    my question is,
    how do i reverse it back to the original file from the binary file(binary_format.txt) created from the above command. the man page of xxd says it cannot be done(in the last line).

    -b | -bits
                  Switch to bits (binary digits) dump, rather than hexdump.   This
                  option  writes octets as eight digits "1"s and "0"s instead of a
                  normal hexadecimal dump. Each line is preceded by a line  number
                  in  hexadecimal and followed by an ascii (or ebcdic) representa‐
                  tion. The command line switches -r, -p, -i do not work with this
                  mode.
    

    if this couldn't be done is there any other command that can do it,. like piping multiple comamnds so.

    • Admin
      Admin over 9 years
      xxd does not output binary files. It outputs textual representations of files. Please read the manual page. The binary format mentioned above is just the bytes of the file written out in 1's and 0's.
  • arvindh
    arvindh over 9 years
    terminal troughs back this: awk: run time error: not enough arguments passed to printf("...%&'") FILENAME="bin_jpg.txt" FNR=59 NR=59. My usage is more or less like recovering the original file,
  • Romeo Ninov
    Romeo Ninov over 9 years
    try to exec it on this way: awk '{printf "%s" $8}' binary_format.txt >out_file
  • arvindh
    arvindh over 9 years
    now the error is, awk: run time error: not enough arguments passed to printf("%s......") FILENAME="bin_jpg.txt" FNR=1 NR=1.
  • Romeo Ninov
    Romeo Ninov over 9 years
    '{printf "%s",$8}' And update your version of awk, seems your is very old
  • arvindh
    arvindh over 9 years
    Should i place these lines inside a file and then run it as a .sh,..?
  • Romeo Ninov
    Romeo Ninov over 9 years
    This will not recover the exact text. It just add enter on each 60th symbol!
  • Allen
    Allen over 9 years
    Why would you be interested only in the eighth field?
  • Allen
    Allen over 9 years
    You can put it in a file or on the command line. This will output the same format as -p: 60 hex values per line.
  • Romeo Ninov
    Romeo Ninov over 9 years
    @Allen, check please how the format (binary) looks like. The original content is in field 8. The rest is counter and binary strings
  • Romeo Ninov
    Romeo Ninov over 9 years
    I see, so you rewrite the coder
  • Allen
    Allen over 9 years
    @R. Ninov: Wrong. That is a textual representation that is not one to one. It uses a period, '.', for anything that doesn't have an ASCII printable character.
  • Romeo Ninov
    Romeo Ninov over 9 years
    @Allen, didn't check that, just test with ascii file
  • arvindh
    arvindh over 9 years
    @Allen, im suppose push 2 different files into another program(i know this sounds lame,.). This particular program accepts only binary representation/format of any file(it doesn't accepts .jpg, .zip etc) that's been given as input. That's the reason i need to convert them into binary. im not an expert, but since i came across with this xxd that can get binary format of a file, i chose this way.
  • Allen
    Allen over 9 years
    In unix, a file is just a stream of bytes. What program are you talking about?
  • Allen
    Allen over 9 years
    xxd takes a file that is a 'binary' format and outputs a TEXTUAL representation of it so it is not what you want anyway.
  • arvindh
    arvindh over 9 years
    Terminal says bash: command substitution: line 18: syntax error near unexpected token $l' bash: command substitution: line 18: bin_to_hex($l))'
  • Allen
    Allen over 9 years
    You have to write your own function bin_to_hex(). You do understand that xxd outputs TEXTUAL representations of files, right?
  • arvindh
    arvindh over 9 years
    @Allen, its just a code written in python, which doesn't have much file handling techniques like buffers or stringIO modules.(nothing huge or impressive about that program/code). so if you think this is not what im suppose to do, you have any suggestions.