How can I convert from hex to base64?

28

6

Can anyone recommend a straightforward way/tool to convert hex to base64?

I'm using Linux and OS X.

Tom Duckering

Posted 2010-06-29T13:41:34.767

Reputation: 413

You want to convert Hex or Binary to Base64? Scriptable or is this a one-shot deal? – Chris S – 2010-06-29T14:17:20.983

Answers

52

Use xxd with the -r argument (and possibly the -p argument) to convert from hex to plain binary/octets and base64 to convert the binary/octet form to base64.

For a file:

cat file.dat | xxd -r -p | base64

For a string of hex numbers:

echo "6F0AD0BFEE7D4B478AFED096E03CD80A" | xxd -r -p | base64

Breton

Posted 2010-06-29T13:41:34.767

Reputation: 671

4

Well, it depends on the exact formatting of your data. But you can do it with a simple shell scripts:

 echo "obase=10; ibase=16; `cat in.dat`" | bc | base64 > out.dat

Modify as needed depending on your data.

pehrs

Posted 2010-06-29T13:41:34.767

Reputation: 357

3That will convert the string of decimal digits. It's not clear if this is what the OP wants or if he has hex digits and wants the bytes they represent converted to base64. – Paused until further notice. – 2010-06-29T14:21:39.303

1

Well, if your hex data is the hex view of a file, just attach the file to a outlook or thunderbird message and then save the message to somewhere. Then open the file with a text editor and see B64 code :)

It functions on Windows, but I think it is a universal way since saving as .EML the attachment is encoded to B64.

kokbira

Posted 2010-06-29T13:41:34.767

Reputation: 4 883