#include #include "utils.h" int Utils::decodeB64(QString strVal) { const char* val = strVal.toStdString().c_str(); //qDebug() << "Decoding B64: " << val << "\n"; int intTot = 0; int y = 0; for (int x = (strVal.length() - 1); x >= 0; x--) { int intTmp = (int) (char) ((val[x] - 64)); if (y > 0) { intTmp = intTmp * (pow(64, y)); } intTot += intTmp; y++; } return intTot; } int Utils::decodeVL64(QString rawq) { int pos = 0; int v = 0; const char* raw = rawq.toStdString().c_str(); bool negative = (raw[pos] & 4) == 4; int totalBytes = raw[pos] >> 3 & 7; v = raw[pos] & 3; pos++; int shiftAmount = 2; for (int b = 1; b < totalBytes; b++) { v |= (raw[pos] & 0x3f) << shiftAmount; shiftAmount = 2 + 6 * b; pos++; } if (negative) { v *= -1; } return v; } QString Utils::encodeVL64(int i) {// switch(i){ // first few are encoded more often then the rest, saves computationaloratories case 0:return QString("H"); case 1:return QString("I"); case 2:return QString("J"); case 3:return QString("K"); default: { byte* wf = new byte[7]; memset(wf, 0, 7); int pos = 0; int startPos = pos; int bytes = 1; int negativeMask = i >= 0 ? 0 : 4; i = fabs(i); // secmath.h fabs? wf[pos++] = (byte) (64 + (i & 3)); for (i >>= 2; i != 0; i >>= 6) { bytes++; wf[pos++] = (byte) (64 + (i & 0x3f)); } wf[startPos] = (byte) (wf[startPos] | bytes << 3 | negativeMask); QString vl64 = QString::fromLocal8Bit((char*) wf); //qDebug() << code << vl64.toLocal8Bit().toHex(); delete[] wf; return vl64; } } }