Base64-to-hexadecimal converter


Earlier a need to convert base64-encoded values to the appropriate hexademical string came up and needed to be done multiple times from the command line in a scripted fashion. Unfortunately the latter requirement prevented me from using a website that does the conversions (at least, without a bit of work) as I usually use this one:

So off to Perl to see how quickly I could conjure something up. With the help of pre-installed and available MIME modules for Perl and some tips from Google on the best way to print data in its hex form this little script came into being. It is not an overly-high performer as it only takes one string per invocation but it could easily be adapted to read from another source and then process multiple times without loading a new Perl environment every time. In the meantime it either takes STDIN base64-encoded values or it will prompt for values. Everything except the actual hash is written to STDERR so you can filter that out and process from there if needed.

ab@mybox0: ~> echo 'RR19QlE2ED2IDSMhVKKjc0mpe4Lxx7nhHgyXBQ1wbKY=' | ./

ab@mybox0: ~> ./ RR19QlE2ED2IDSMhVKKjc0mpe4Lxx7nhHgyXBQ1wbKY=
(Visited 1 times, 1 visits today)


  • dimstar says:

    What stopped you from using something like:

    echo RR19QlE2ED2IDSMhVKKjc0mpe4Lxx7nhHgyXBQ1wbKY= | base64 -d – | hexdump -C

    in your script? hexdump itself can also be given more parameters to give you a better format for you, if needed. (don’t just know what format you need the output in… hexdump allows almost all you could imagine )

  • dimstar says:

    The followin command will print as result:

    echo RR19QlE2ED2IDSMhVKKjc0mpe4Lxx7nhHgyXBQ1wbKY= | base64 -d – | hexdump -e ‘/1 “%x” ‘


    The hex representation can be changed of course, like for example 16 bytes then a new line, spaces between the bytes and all what not…

    echo RR19QlE2ED2IDSMhVKKjc0mpe4Lxx7nhHgyXBQ1wbKY= | base64 -d – | hexdump -e ’16/1 “%02x ” ‘ -e ‘ “n” ‘

    for example?

  • aburgemeister says:

    I appreciate the feedback. I wasn’t able to figure out the hexdump part that you were but instead had all of the bytes swapped around and rather than write something to translate them back (or figure out the hexdump part) I went with Perl.

    Thanks for the feedback.