Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 encoding of String in Qt 5

Tags:

c++

base64

qt

qt5

I am trying to base64 encode a QString in Qt5 . However, I am getting an error saying identifier not found on line QString b64string = base64_encode(src);

#include <QCoreApplication>
#include <QByteArray>
#include <QBitArray>
#include <QString>
#include <QDebug>

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


    QString src = "Hello";
    QString b64string = base64_encode(src);

    qDebug() << "Encoded string is" << b64string;
    return a.exec();
}

QString base64_encode(QString string){
    QByteArray ba;
    ba.append(string);
    return ba.toBase64();
}

Why is the error occurring? can someone point out my mistake?

like image 422
Parth Doshi Avatar asked Jan 28 '14 16:01

Parth Doshi


2 Answers

The problem you face is what Mark Ransom said , just change the order of the functions or write a function prototype at the beginning of the file to solve your issue. but When i want base 64 i usually do this

QString src = "Hello";
src.toUtf8().toBase64();

so you do't have to write a custom function .

like image 113
Midhun Avatar answered Oct 23 '22 08:10

Midhun


The identifier it can't find is base64_encode. This is because it doesn't come until later in the file. The usual way of preventing this error is to put a function prototype at the beginning of the file or in a separate include header:

QString base64_encode(QString string);

You could also just rearrange the code so that anything depending on the definition comes last, i.e. move main to the end.

like image 34
Mark Ransom Avatar answered Oct 23 '22 09:10

Mark Ransom