6

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.

arvindh
  • 163
  • 1
  • 1
  • 3
  • 1
    `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. – Allen Jan 17 '15 at 19:58

2 Answers2

1

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.

Allen
  • 166
  • 6
0

You alter the structure of the file do you can't easy do this. Maybe something like this can help (but you will loose newline characters:

awk '{printf $8}' binary_format.txt >out_file
Romeo Ninov
  • 5,319
  • 5
  • 20
  • 20
  • 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, – arvindh Jan 17 '15 at 17:57
  • try to exec it on this way: awk '{printf "%s" $8}' binary_format.txt >out_file – Romeo Ninov Jan 17 '15 at 18:07
  • now the error is, `awk: run time error: not enough arguments passed to printf("%s......") FILENAME="bin_jpg.txt" FNR=1 NR=1`. – arvindh Jan 17 '15 at 18:37
  • '{printf "%s",$8}' And update your version of awk, seems your is very old – Romeo Ninov Jan 17 '15 at 18:42
  • Why would you be interested only in the eighth field? – Allen Jan 17 '15 at 18:51
  • @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 Jan 17 '15 at 18:58
  • @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. – Allen Jan 17 '15 at 19:06
  • @Allen, didn't check that, just test with ascii file – Romeo Ninov Jan 17 '15 at 19:11
  • @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. – arvindh Jan 17 '15 at 19:23
  • In unix, a file is just a stream of bytes. What program are you talking about? – Allen Jan 17 '15 at 19:34
  • xxd takes a file that is a 'binary' format and outputs a TEXTUAL representation of it so it is not what you want anyway. – Allen Jan 17 '15 at 19:36
  • @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. – arvindh Jan 17 '15 at 20:00