7 static QString keystring = QString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
9 QString toBase64(QByteArray binary) {
12 int pad = binary.size() % 3;
13 for(i = 0; i < binary.size(); i+=3) {
18 retval.append(keystring[(a & 0xfc) >> 2 ]);
19 if( i+1 < binary.size()) {
21 quint8 idex = ((a & 0x03) << 4) | ((b & 0xf0) >> 4);
22 retval.append(keystring[idex]);
23 if (i+2 == binary.size()) {
24 retval.append(keystring[(b & 0x0f) << 2]);
27 retval.append(keystring[(a & 0x03) << 4]);
30 if( i+2 < binary.size()) {
32 quint8 idex = ((b & 0x0f) << 2) | ((c & 0xc0) >> 6);
33 retval.append(keystring[idex]);
34 retval.append(keystring[c & 0x3f]);
44 QByteArray fromBase64(QString text) {
46 if( text.size() % 4 != 0)
48 for(int i = 0; i < text.size(); i+=4) {
49 quint8 idex1 = static_cast<quint8>(keystring.indexOf(text[i])); // Something between 0 and 63
50 quint8 idex2 = static_cast<quint8>(keystring.indexOf(text[i+1]));
51 quint8 idex3 = static_cast<quint8>(keystring.indexOf(text[i+2]));
52 quint8 idex4 = static_cast<quint8>(keystring.indexOf(text[i+3]));
53 retval.append( ((idex1 << 2) | ((idex2 & 0x30) >> 4))) ;
56 retval.append( ((idex2 & 0x0f) << 4) | ((idex3 & 0x3c) >> 2) ) ;
59 retval.append( ((idex3 & 0x03) << 6) | (idex4) ) ;
68 QByteArray test = QByteArray("Texas!");
70 qDebug() << toBase64(test);
71 qDebug() << fromBase64(toBase64(test));