views:

160

answers:

8

I'm looking for an easy way to convert a simple binary file into a text-representation of its binary, where encoding doesn't matter. I know that the programmatic solution is straightforward, but I feel that there must be some arcane string of unix commands to accomplish this.

Am I off base? Is there a simpler solution than the programmatic?

+7  A: 

Use od. For example:

$ od -t x1 -An /bin/ls | head
 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
 02 00 3e 00 01 00 00 00 e0 26 40 00 00 00 00 00
 40 00 00 00 00 00 00 00 30 b6 01 00 00 00 00 00
 00 00 00 00 40 00 38 00 09 00 40 00 1d 00 1c 00
 06 00 00 00 05 00 00 00 40 00 00 00 00 00 00 00
 40 00 40 00 00 00 00 00 40 00 40 00 00 00 00 00
 f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00
 08 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00
 38 02 00 00 00 00 00 00 38 02 40 00 00 00 00 00
 38 02 40 00 00 00 00 00 1c 00 00 00 00 00 00 00
Greg Bacon
Is there any way to get rid of that address location output? I'm reading through man od now.
Stefan Kendall
Yes! You can use `cut` as in catwalk's answer or use the `-A` option to `od`.
Greg Bacon
+5  A: 

uuencode and uudecode were made for transferring binary content as ASCII characters. See the wikipedia entry.

Ewan Todd
+6  A: 

for example, to display a binary file as a sequence of hex codes:

od -t x1 file|cut -c8-
catwalk
+6  A: 
base64 -e filename>xxx

on the other side

base64 -d xxx>filename
ammoQ
Yep. Stefan, assuming you want to be able to reverse this conversion, you want `base64` or `uuencode`, which were actually designed for this task.
Jason Orendorff
+5  A: 
max@upsight:~$ openssl base64 < /dev/urandom | head -10
qnISxigXTjgON+tkSDtRJ6fRNczsejY2bEC5D1W8fscy+6mopiGfVLvZ/bu99SrT
qdTRaeRXO8fgEejXsbTy4XP9MmCbAsBCSEvDpq5bfR/Sd7EjJLUxcRwzEMlhIrYT
m6J+20aR9M4g7pbT+hjjBE/gsHKxFfZQFgxT/tm1pEg6zMvQywjsrc7d+PSJQOHw
vzYXfWkyLO1nJm9g+Pw3rBI/UuV0+lmrIflhlj5CDWuaxDJUXJiWdsD6cGKLclfz
Mlh17mHwteqMLLSrTZ0QA0ygxISqiCf2sDtPgUToM7ZT2EbaNck5auxbhU+7OcxI
vBZRKozRZtfsZA0IUzMlIQmFanBdjOeGepQjgCDruq5hqEbNc1A+HhXqTtAr8Aic
4iNf36xZifDvASYy27hTVrlI/5kTeRZURqquaxHqum15VD5IC3J/sH+AwPpN1/qi
0YM8xt+LliVje7Oo7QiTona+VMjA//a715/0J8yeryLxTLSnT8JsXUpR0CiOgAcH
tQk9nzHCfMmFzb02nrhFJ0MjLCFgNJOiI1vT0AhNnMh449dcIkDDwyMpkRV4KZ1l
CSL+K4vXhMz3LhPKSihKbYLY6aJSnlPe/GiIOfl1g1VlbtoxJ7ZclpcOp4KWSKHV

...and so on

Max A.
+1  A: 

If the reason you're doing it is to see strings inside the binary data then there's a command called "strings" that will print all the strings in a file for you.

Mattias Nilsson
I was just about to comment on this exact subject. Good thing I read the previous answers first.
R. Bemrose
Nope, not my purpose.
Stefan Kendall
A: 

Yes, you are off-base, this is nontrivial in the general case. Some commercial solutions exist, one we use is Autonomy Keyview.

I am assuming you mean including (e.g.) MSOffice and PDFs.

MarkR
+1  A: 

you can also use hexdump. Look at the man page for more options

$ hexdump binaryfile