Breaking QByteArray::toBase64() results into 64 character lines
It would be nice if QByteArray::toBase64() had an option to split the results into multiple fixed-length lines.
Currently its results are just in a single line (i.e. with no line breaks). (We actually have a requirement for this; my group's model serialization can't have arbitrarily long lines). The documentation states that this function conforms to RFC 2045 which seems to require lines of no more that 76 characters, but there may be a technical reason why this isn't always required. But still, it would be nice. (Apparently some other Base64 encodings require line lengths of 64 characters, so that's a good general length).
Could QByteArray::toBase64() be enhanced with an optional integer parameter of the maximum line length, with 0 as the default for the current single-line behavior?
Here is a function we are now using to do this line breaking:
@QByteArray UserImageData::wrapAt64Chars (const QByteArray& inArray)
// This function takes a QByteArray lacking line breaks and returns
// that QByteArray value with line breaks inserted after every
// 64 bytes. It is intended for use with Base64-encoded QByteArrays
// created with QByteArray::toBase64() which generates a Base64 string
// without line breaks [as of Qt 4.8.5].
// Note that the QByteArray::fromBase64() static method ignores all
// characters which are not part of the encoding, including CR and LF.
// This is a provision of the "RFC 2045" Base64 standard supported by
// these methods. RFC 2045 specficies a maximum line length of 76
// characters. See: http://en.wikipedia.org/wiki/Base64
// The code below was adapted from this method in Qt 4.8:
// ByteArray QSslCertificatePrivate::QByteArray_from_X509();
const int inSize = inArray.size();
const int outEst = inSize + (inSize/32) + 4;
const char* inPtr = inArray.data();
for (int inCrs = 0; inCrs <= inSize - 64; inCrs += 64)
outArray += QByteArray::fromRawData (inPtr + inCrs, 64);
outArray += "\n";
const int rem = inSize % 64;
if (rem > 0)
outArray += QByteArray::fromRawData (inPtr + inSize - rem, rem);
outArray += "\n";
For this kind of demand, you should create a new feature request "here":http://bugreports.qt-project.org . Asking this on the forum will likely get lost in the long run because it's not closely followed by Qt's maintainers/developers, it's more user oriented.
Happy coding :)