Stefan Kendall
Stefan Kendall

Reputation: 67802

Unix wizardry to get the binary content of a file as text?

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?

Upvotes: 3

Views: 3044

Answers (8)

ghostdog74
ghostdog74

Reputation: 342333

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

$ hexdump binaryfile

Upvotes: 1

MarkR
MarkR

Reputation: 63538

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.

Upvotes: -1

Mattias Nilsson
Mattias Nilsson

Reputation: 3757

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.

Upvotes: 1

Greg Bacon
Greg Bacon

Reputation: 139441

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

Upvotes: 7

Max A.
Max A.

Reputation: 4882

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

Upvotes: 5

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36977

base64 -e filename>xxx

on the other side

base64 -d xxx>filename

Upvotes: 7

catwalk
catwalk

Reputation: 6476

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

od -t x1 file|cut -c8-

Upvotes: 6

Ewan Todd
Ewan Todd

Reputation: 7312

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

Upvotes: 6

Related Questions