Results 1 to 3 of 3

Thread: Breaking QByteArray::toBase64() results into 64 character lines

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. QByteArray UserImageData::wrapAt64Chars (const QByteArray& inArray)
    2. {
    3. // This function takes a QByteArray lacking line breaks and returns
    4. // that QByteArray value with line breaks inserted after every
    5. // 64 bytes. It is intended for use with Base64-encoded QByteArrays
    6. // created with QByteArray::toBase64() which generates a Base64 string
    7. // without line breaks [as of Qt 4.8.5].
    8. //
    9. // Note that the QByteArray::fromBase64() static method ignores all
    10. // characters which are not part of the encoding, including CR and LF.
    11. // This is a provision of the "RFC 2045" Base64 standard supported by
    12. // these methods. RFC 2045 specficies a maximum line length of 76
    13. // characters. See: http://en.wikipedia.org/wiki/Base64
    14. //
    15. // The code below was adapted from this method in Qt 4.8:
    16. // ByteArray QSslCertificatePrivate::QByteArray_from_X509();
    17.  
    18. const int inSize = inArray.size();
    19. const int outEst = inSize + (inSize/32) + 4;
    20. QByteArray outArray;
    21. outArray.reserve (outEst);
    22.  
    23. const char* inPtr = inArray.data();
    24. for (int inCrs = 0; inCrs <= inSize - 64; inCrs += 64)
    25. {
    26. outArray += QByteArray::fromRawData (inPtr + inCrs, 64);
    27. outArray += "\n";
    28. }
    29.  
    30. const int rem = inSize % 64;
    31. if (rem > 0)
    32. {
    33. outArray += QByteArray::fromRawData (inPtr + inSize - rem, rem);
    34. outArray += "\n";
    35. }
    36.  
    37. return (outArray);
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by philw; 28th October 2013 at 03:31.

Similar Threads

  1. Replies: 2
    Last Post: 11th October 2011, 14:55
  2. Weird results when passing a QByteArray* to a method
    By agerlach in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2010, 22:05
  3. Qt Designer Problem moving widget after breaking layout
    By nickolais in forum Qt Tools
    Replies: 0
    Last Post: 10th June 2010, 22:21
  4. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  5. NEED HELP: How to download file without breaking function
    By codeslicer in forum Qt Programming
    Replies: 10
    Last Post: 22nd April 2008, 04:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.