1

There's a nice CLI binutils utility for interpreting binary files, and outputting legible strings, in a cat-like fashion. It's called strings.

If you try to cat a binary file, you'll see a bunch of garbled text and alien symbols. And probably have to reset your shell. Which is sometimes not even possible

Just like cat, basic usage is simply:

strings ./example.bin

Personally, for quick perusal, I like to:

strings -aws ' ' ./example.bin | fold -sw $COLUMNS ; echo

If you want to test it on a real file, you can try something in the /bin directory, like:

strings -aws ' ' /bin/true | fold -sw $COLUMNS ; echo

My question: Is there a similar, quick & easy method for editing these strings?

  • You might not want to edit binary files in a similar fashion. They will become garbled. – Rui F Ribeiro Jun 17 '18 at 9:00
  • @tjt263. Yes, use a hex editor. – fpmurphy Jun 17 '18 at 11:27
2

Yes. vim -b filename. The -b signifies binary file. See man page for vim.

   -b          Binary mode.  A few options will be set that makes it
               possible to  edit  a  binary  or  executable file.

bvi also suitable.

$ bvi /bin/ls
00000000  7F 45 4C 46 02 01 01 00 00 00 00 00 00 00 00 00 02 00 3E 00 01 00 00 00 .ELF..............>.....
00000018  A0 49 40 00 00 00 00 00 40 00 00 00 00 00 00 00 38 E7 01 00 00 00 00 00 .I@.....@.......8.......
00000030  00 00 00 00 40 00 38 00 09 00 40 00 1D 00 1C 00 06 00 00 00 05 00 00 00 ....@.8...@.............
00000048  40 00 00 00 00 00 00 00 40 00 40 00 00 00 00 00 40 00 40 00 00 00 00 00 @.......@.@.....@.@.....
00000060  F8 01 00 00 00 00 00 00 F8 01 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ........................
1

I stumbled upon this nice article about binary data manipulation: Manipulating Binary Data in Bash

It mostly explains a very handy tool called xxd

For example to dump a binary file in ASCII, you'll do:

xxd -b myfile.bin

To convert a short string, you could use the good old bc:

$ echo "obase=16; ibase=2; 1010" | bc
A

Another method to consider:

$ base64 < binary.file > text.file

ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10.

  • I tried to convert my answer to a comment with no luck! – Sam Jun 17 '18 at 10:12

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.