141

I have this binary file on my Linux system...

 udit@udit-Dabba ~ $ cat file.enc
 Salted__s�bO��<0�F���Jw!���]�:`C�LKȆ�l

Using the hexdump command, I see its information like this:

 udit@udit-Dabba ~ $ hexdump -C file.enc
 00000000  53 61 6c 74 65 64 5f 5f  1b 73 a1 62 4f 15 be f6  |Salted__.s.bO...|
 00000010  3c 30 cc 46 ee 10 13 11  84 bf 4a 77 21 a4 84 99  |<0.F......Jw!...|
 00000020  0e 5d ef 11 18 3a 60 43  a0 4c 4b 1e c8 86 e6 6c  |.]...:`C.LK....l|
 00000030

Now I am given a file on some other system whose contents are like this:

 53 61 6c 74 65 64 5f 5f  1b 73 a1 62 4f 15 be f6
 3c 30 cc 46 ee 10 13 11  84 bf 4a 77 21 a4 84 99
 0e 5d ef 11 18 3a 60 43  a0 4c 4b 1e c8 86 e6 6c

And I need to find out that same exact binary information from this hexdump.

How can I proceed for that?

If there isn't any switch for that then C code will also work fine.

(But a Linux command with some switch is preferable)

Limitation:

The binary information in the file is output of an encryption algorithm, so contents should exactly match...

4
  • 14
    Have a look at xxd Commented Oct 19, 2011 at 19:00
  • @user786653 Thnx It helped I knew about -r and not -p and needed it soon ... so posted the question Commented Oct 19, 2011 at 19:22
  • 1
    About the same subject, see also: stackoverflow.com/questions/2614764/hex-dump-of-file-in-bash Commented Aug 28, 2014 at 9:58
  • FYI, if you're just wanting to move binary data around but are limited to ASCII, consider base64 from coreutils or even uuencode/uudecode from the sharutils package. Commented Jan 14 at 20:56

2 Answers 2

254

As @user786653 suggested, use the xxd(1) program:

xxd -r -p input.txt output.bin
Sign up to request clarification or add additional context in comments.

5 Comments

to add use echo "hex" | xxd -r -p - to take input from piped standard out
Can also be used to get binary hashes - like echo something | sha256sum | xxd -r -p -
Note that xxd uses very old fashioned argument processing, so all of the following behave differently: xxd -rp xxd -pr xxd -r -p. Only the last of the three works for this use case.
@Amanpreet no need to do that because the man page says that If no infile is given, standard input is read.
3

Python stdlib solution

If for some unfathomably enterprisey reason you can't sudo apt install xxd, it is easy to reimplement it in Python as per: How to create python bytes object from long hex string? with:

xxd2() ( python -c "import sys;import fileinput;sys.stdout.buffer.write(bytes.fromhex(''.join(fileinput.input(sys.argv[1:]))))" "$@" )

which works both with files and stdin:

printf 01ab | xxd2 
printf '01 ab' | xxd2

or:

printf 01ab > myfile.hex
xxd2 myfile.hex

Here's the script with better indentation:

import sys
import fileinput
sys.stdout.buffer.write(
    bytes.fromhex(
        ''.join(
            fileinput.input(sys.argv[1:])
        )
    )
)

The bytes.fromhex function ignores whitespaces and newlines since Python 3.7, so it works regardless of the indentation details of the format, as per docs: https://docs.python.org/3.12/library/stdtypes.html#bytes.fromhex

Changed in version 3.7: bytes.fromhex() now skips all ASCII whitespace in the string, not just spaces.

Tested on Python 3.12.3, Ubuntu 24.04.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.