Base64 Encode and Decode using C++

Qt Core module

  1. Add linking against the module library:

If building project with CMake then use the find_package() command to locate the needed module:

find_package(Qt5 COMPONENTS Core REQUIRED)
target_link_libraries(myapp Qt5::Core)
  1. Encode and decode data using Base64:
#include <iostream>
#include <QCoreApplication>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QString text = "Hello";
    QString base64Str = text.toUtf8().toBase64();
    std::cout << base64Str.toStdString() << std::endl;

    text = QString(QByteArray::fromBase64(base64Str.toUtf8()));
    std::cout << text.toStdString() << std::endl;

    return app.exec();
}

Leave a Comment

Cancel reply

Your email address will not be published.