9

I can find no sample code to convert a Base64 string to a hexadecimal string, both in the most readable format, such as

5c2e00405e56439da66a4bd279dbfba2d605d6949142ade664ce0fc4a7ceee
0a9f0c5f5b8a13bfe6431fe48de5b7935b6604946c811e3b836f77c28f059df801

and

XC4AQF5WQ52makvSedv7otYF1pSRQq3mZM4PxKfO7gqfDF9bihO/5kMf5I3lt5NbZgSUbIEeO4Nvd8KPBZ34AQ==

Does such an open source library exist? If so, where?

  • 1
    You could ask this question on stackoverflow... better chances of getting best answers to your question. That site's all about programming! – mk117 Jun 29 '14 at 9:30
  • Does this code help you with the conversion? – mk117 Jun 29 '14 at 16:55
  • Well, I searched again for "Free libraries" as per your question, and found this library called Crypto++ ... It supports Base64 and Hex conversion, as listed under features...: hex, base-32, and base-64 coding/decoding ... I don't know how it does that, but here's a link to the reference documentation available on that website. – mk117 Jun 29 '14 at 17:07
  • Done! Answer posted with features and links to download and reference... – mk117 Jun 29 '14 at 17:29
5

Crypto++® Library 5.6.2

Crypto++ Library is a free C++ class library of cryptographic schemes.

Other features include:

  • pseudo random number generators (PRNG): ANSI X9.17 appendix C, RandomPool
  • password based key derivation functions: PBKDF1 and PBKDF2 from PKCS #5, PBKDF from PKCS #12 appendix B
  • Shamir's secret sharing scheme and Rabin's information dispersal algorithm (IDA)
  • fast multi-precision integer (bignum) and polynomial operations
  • finite field arithmetics, including GF(p) and GF(2^n)
  • prime number generation and verification
  • useful non-cryptographic algorithms
    • DEFLATE (RFC 1951) compression/decompression with gzip (RFC 1952) and zlib RFC 1950) format support
    • hex, base-32, and base-64 coding/decoding
    • 32-bit CRC and Adler32 checksum class wrappers for these operating system features (optional):
    • high resolution timers on Windows, Unix, and Mac OS
    • Berkeley and Windows style sockets
    • Windows named pipes
    • /dev/random, /dev/urandom, /dev/srandom
    • Microsoft's CryptGenRandom on Windows
  • A high level interface for most of the above, using a filter/pipeline metaphor
  • benchmarks and validation testing
  • x86, x86-64 (x64), MMX, and SSE2 assembly code for the most commonly used lgorithms, with run-time CPU feature detection and code selection
    • supports GCC-style and MSVC-style inline assembly, and MASM for x64
  • certain versions are available in FIPS 140-2 validated form

Download:
http://www.cryptopp.com/#download

Manual:
http://www.cryptopp.com/docs/ref/

  • Wow, really well-written answer! – user5034 Jun 29 '14 at 17:29
  • @Cincinnatus Very-well copied it seems – follow the link to see the original. // mk117: copy-pasted content might cause copyright issues, and thus in this form is not very welcome on SE sites. You should always make clear what's a copy and what's your own content. The latter is btw missing in your answer – e.g. personal experience with this product ;) – Izzy Aug 4 '14 at 6:02
  • @Izzy lol, I went straight to the manual. – user5034 Aug 4 '14 at 12:03
  • For me, the "Download" button raised an eyebrow. Then I saw e-sushi's edit (credits to him for detecting the copy-pasta), and knew the reason ;) – Izzy Aug 4 '14 at 12:10
2

Not too sure about doing it in C++ but in python it is easy:

#!/usr/bin/env python

import base64
import binascii

def Print64Hex(instring):
    """ Prints a base 64 string as hex."""
    data = base64.standard_b64decode(instring)
    print binascii.hexlify(data)

if __name__ == "__main__":
    import sys
    for d in sys.argv[1:]:
        Print64Hex(d)

So if you just need a utility to do this get python, it is also reasonably simple to embed python within your C++ program and vice-versa so using python as a library may be an option for you, see embedding for python 2 or for python3 note that for Python3 you need to change print binascii.hexlify(data) to print(binascii.hexlify(data)) in the code above.

1

You don't need any 3rd party library if you use Qt:

#include <QByteArray>

...

QByteArray str1 = "5c2e00405e56439da66a4bd279dbfba2d605d6949142...";
QByteArray hex = QByteArray::fromHex(str1);

QByteArray str2 = "XC4AQF5WQ52makvSedv7otYF1pSRQq3mZM4PxKfO7gqfDF9bihO...";
QByteArray base64 = QByteArray::fromBase64(str2);

QByteArray hexStr = str2.toHex();
  • Just to be sure I’m not mistaking: isn’t Qt – being a framework for C++ and QML – “3rd party” too? – e-sushi Aug 4 '14 at 4:30
1

As an alternative to the already mentioned, potential solutions…

libb64: Base64 Encoding/Decoding Routines

libb64 is a library of ANSI C routines for fast encoding/decoding data into and from a base64-encoded format. C++ wrappers are included, as well as the source code for standalone encoding and decoding executables.

License:
Public Domain.

Download:
http://sourceforge.net/projects/libb64/files/latest/download?source=files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy