1
1
Note: I have marked this question as a duplicate of another question. But I am keeping it nonetheless as it has an example and a clearly explained answer, so hopefully it should help others.
-- I need to convert a string of Hex characters into base64 as done by this online convertor in *nix.
For "5C78336D77D8DF448007D277DAD5C569
"(Hex) I know the expected output is "XHgzbXfY30SAB9J32tXFaQ==
"(base64).
But when I try converting it into binary and then base64 I get this:
[kent@server SrcFiles]$ echo "5C78336D77D8DF448007D277DAD5C569" | xxd -b
0000000: 00110101 01000011 00110111 00111000 00110011 00110011 5C7833
0000006: 00110110 01000100 00110111 00110111 01000100 00111000 6D77D8
000000c: 01000100 01000110 00110100 00110100 00111000 00110000 DF4480
0000012: 00110000 00110111 01000100 00110010 00110111 00110111 07D277
0000018: 01000100 01000001 01000100 00110101 01000011 00110101 DAD5C5
000001e: 00110110 00111001 00001010 69.
[kent@server SrcFiles]$ echo "001101010100001100110111001110000011001100110011001101100100010000110111001101110100010000111000010001000100011000110100001101000011100000110000001100000011011101000100001100100011011100110111010001000100000101000100001101010100001100110101001101100011100100001010" | base64
MDAxMTAxMDEwMTAwMDAxMTAwMTEwMTExMDAxMTEwMDAwMDExMDAxMTAwMTEwMDExMDAxMTAxMTAw
MTAwMDEwMDAwMTEwMTExMDAxMTAxMTEwMTAwMDEwMDAwMTExMDAwMDEwMDAxMDAwMTAwMDExMDAw
MTEwMTAwMDAxMTAxMDAwMDExMTAwMDAwMTEwMDAwMDAxMTAwMDAwMDExMDExMTAxMDAwMTAwMDAx
MTAwMTAwMDExMDExMTAwMTEwMTExMDEwMDAxMDAwMTAwMDAwMTAxMDAwMTAwMDAxMTAxMDEwMTAw
MDAxMTAwMTEwMTAxMDAxMTAxMTAwMDExMTAwMTAwMDAxMDEwCg==
Can anyone point me in the right direction?
1
Converting a Base 16 value to binary will keep it Base 16. You need to convert the Base 16 to Base 64 then convert it to Binary. This has been asked and answered before http://superuser.com/questions/158142/how-can-i-convert-from-hex-to-base64?rq=1
– Ramhound – 2013-07-25T16:04:44.617@Ramhound - Thanks. I tried
echo "obase=10; ibase=16;
cat in.dat" | bc | base64 > out.dat
andecho "obase=64; ibase=16;
cat in.dat" | bc
. Could you give me some pointers where I am going wrong..? – Kent Pawar – 2013-07-25T16:14:17.980Your syntax is wrong. Look up the correct syntax. – Ramhound – 2013-07-25T16:18:18.227
1As a comment to the original question,
echo "0011010..." | base64
does not send the binary string of 0011010... but, the string of ascii 0 and ascii 1 characters. That is why your output is not what you expect. – Kent – 2013-07-26T07:01:24.857Thanks @Kent ! okay. But then shouldn't this work too..?
echo "obase=64; ibase=16; 5C78336D77D8DF448007D277DAD5C569"
# Tellbc
to accept the input ASCII string as a Hex representation and convert it into base64. – Kent Pawar – 2013-07-26T07:12:55.5371bc is not doing what you expect it to do either. As a numeric processor, its value for 0, 1, 2 in base 64 is still 0, 1, 2. However, the base64 encoding of 0 is A, 1=B, 2=C, etc. It is arbitrary; and, since
bc
is a calculator, it won't make that arbitrary conversion for us. I have some more to say on this, but, it requires formatting and much more than 512 characters. I'll add it to the answer section below, even though it doesn't answer your original question. – Kent – 2013-07-31T00:44:45.350