How to convert (const std::vector<unsigned char>& vchIn) to QByteArray?
-
I have a function from a library which requires a const std::vector<unsigned char> as the second argument. The function declaration looks like this:
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
My first parameter is a QByteArray and I would like to have the result also in a QByteArray. How can I convert
const std::vector<unsigned char>& vchIn
into a QByteArray? Or even better, how could I change the function below, that it returns a QByteArray?static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) { // Skip leading spaces. while (*psz && isspace(*psz)) psz++; // Skip and count leading '1's. int zeroes = 0; int length = 0; while (*psz == '1') { zeroes++; psz++; } // Allocate enough space in big-endian base256 representation. int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. std::vector<unsigned char> b256(size); // Process the characters. while (*psz && !isspace(*psz)) { // Decode base58 character const char* ch = strchr(pszBase58, *psz); if (ch == nullptr) return false; // Apply "b256 = b256 * 58 + ch". int carry = ch - pszBase58; int i = 0; for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) { carry += 58 * (*it); *it = carry % 256; carry /= 256; } assert(carry == 0); length = i; psz++; } // Skip trailing spaces. while (isspace(*psz)) psz++; if (*psz != 0) return false; // Skip leading zeroes in b256. std::vector<unsigned char>::iterator it = b256.begin() + (size - length); while (it != b256.end() && *it == 0) it++; // Copy result into output vector. vch.reserve(zeroes + (b256.end() - it)); vch.assign(zeroes, 0x00); while (it != b256.end()) vch.push_back(*(it++)); return true; }
-
-
@Christian-Ehrlicher 's solution is the way to go. I got complicated on mine:
std::vector<unsigned char> source = {'a'+0,'a'+1,'a'+2,'a'+3,'a'+4,'a'+5,'a'+6,'a'+7,'a'+8,'a'+9}; QByteArray output; // empty byte array for(auto val: source){ output.push_back(val); } // or use something from algorithm QByteArray output2; std::copy(source.begin(), source.end(), std::back_inserter(output2)); qInfo() << output; qInfo() << output2;
-
That is how I ended up doing it:
QByteArray encodeInput = "Hello world"; std::vector<unsigned char> encodeResultVector; encodeResultVector.reserve(encodeInput.size()); for(int i = 0; i < encodeInput.size(); ++i) encodeResultVector.push_back(static_cast<unsigned char>(encodeInput.at(i))); std::string encodeReslutStdString = EncodeBase58(encodeResultVector); QByteArray encodedArray(encodeReslutStdString.c_str(), encodeReslutStdString.length()); qDebug() << "encode input :" << encodeInput; qDebug() << "encoded array :" << encodedArray; QByteArray decodedArray; std::vector<unsigned char> vchRet; bool ret = DecodeBase58(encodedArray.constData(), vchRet); decodedArray.reserve(vchRet.size()); for(size_t i = 0; i < vchRet.size(); ++i) decodedArray.append(vchRet.at(i)); qDebug() << "decoded array :" << decodedArray;
encode input : "Hello world" encoded array : "JxF12TrwXzT5jvT" decoded array : "Hello world"
-
@QtCoder87 said in How to convert (const std::vector<unsigned char>& vchIn) to QByteArray?:
why not bytearry from Std::vector<uint8_t> ?
Mainly because there was little demand for it.
It is very easy to and cheap to reinterpret a
uint8_t
array as achar
array, soQByteArray::fromStdUInt8Vector(const std::vector<uint_8> &vector)
wouldn't add much value. Just useQByteArray(const char *data, int size)
.Anyway, future code should not use
uint8_t
ORchar
. Instead, we should move towardsstd::byte
.