4

I have byte data,

0Y0*†HÎ*†HÎ=B¬`9E>ÞÕ?ÐŽ·‹ñ6ì­Â‰&ÉÐL_cüsyxoú¢
†
(¯-›E–¿9ÐÖ1ªÐ›¯o

Now, I want to create one Linux script that will convert that data into a hex string. How can I do that?

3
  • that bunch of binary data is in a file? Commented Mar 1, 2012 at 11:06
  • that data is in one file Commented Mar 1, 2012 at 11:07
  • if u're using ubuntu. it's quite easy to open Python and type some commands Commented Mar 1, 2012 at 11:09

3 Answers 3

12

Should you want only the hex strings:

$ echo '0Y0*†HÎ*†HÎ=B¬`9E>ÞÕ?ÐŽ·‹ñ6ì­Â‰&ÉÐL_cüsyxoú¢'|od -vt x1|awk '{$1="";print}'
 30 59 30 2a e2 80 a0 48 c3 8e 2a e2 80 a0 48 c3
 8e 3d 42 c2 ac 60 39 45 3e c3 9e c3 95 3f c3 90
 c5 bd c2 b7 e2 80 b9 c3 b1 36 c3 ac c2 ad c3 82
 e2 80 b0 26 c3 89 c3 90 4c 5f 63 c3 bc 73 79 78
 6f c3 ba c2 a2 0a

You can avoid the awk part by just using od -vt x1 -A n. Thanks @Stefan van den Akker.

Sign up to request clarification or add additional context in comments.

1 Comment

My GNU od version 9.0 has the flags -A n which skips the offset so you don't have to filter through awk.
8

Use the hd (hex dump) command:

$ echo '0Y0*†HÎ*†HÎ=B¬`9E>ÞÕ?ÐŽ·‹ñ6ì­Â‰&ÉÐL_cüsyxoú¢' | hd
00000000  30 59 30 2a e2 80 a0 48  c3 8e 2a e2 80 a0 48 c3  |0Y0*...H..*...H.|
00000010  8e 3d 42 c2 ac 60 39 45  3e c3 9e c3 95 3f c3 90  |.=B..`9E>....?..|
00000020  c5 bd c2 b7 e2 80 b9 c3  b1 36 c3 ac c2 ad c3 82  |.........6......|
00000030  e2 80 b0 26 c3 89 c3 90  4c 5f 63 c3 bc 73 79 78  |...&....L_c..syx|
00000040  6f c3 ba c2 a2 0a                                 |o.....|
00000046

Or, if you don't have hd, hexdump:

$ echo '0Y0*†HÎ*†HÎ=B¬`9E>ÞÕ?ÐŽ·‹ñ6ì­Â‰&ÉÐL_cüsyxoú¢' | hexdump 
0000000 5930 2a30 80e2 48a0 8ec3 e22a a080 c348
0000010 3d8e c242 60ac 4539 c33e c39e 3f95 90c3
0000020 bdc5 b7c2 80e2 c3b9 36b1 acc3 adc2 82c3
0000030 80e2 26b0 89c3 90c3 5f4c c363 73bc 7879
0000040 c36f c2ba 0aa2                         
0000046

4 Comments

hd B_Party_DEFAULT_CERTIFICATE_Public.der bash: hd: command not found Not working
sudo apt-get install bsdmainutils (on Debian/Ubuntu).
you can use hexdump -C (I have RedHat and it has hexdump but not hd)
hd is non-standard, but one can always use hexdump -C (shipped in util-linux) to get the same.
2

If you want just he hex bytes without any formatting:

od -An -v -tx1 | tr -d ' \n'

Example:

printf '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' | od -An -v -tx1 | tr -d ' \n'

Output:

000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

Tested on Ubuntu 23.04.

Related: How to create a hex dump of file containing only the hex characters without spaces in bash?

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.