Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert QByteArray to QString
Forum Updated to NodeBB v4.3 + New Features

Convert QByteArray to QString

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    javad_2012
    wrote on last edited by
    #1

    Hi everyone!
    I want to encrypt the data of a database and to do this, I used AES_128 this link for encryption.

    The result of encryption is a QByteArray. The QByteArray is saved on the text file in the correct shape and I could decode it correctly, but I need to save it on the Sqlite DB. To do this, I should convert it to the QString, save it and do a reverse loop for reading from DB. I tried some options like

    QByteArray encodedText; QString DataAsString = QString(encodedText);
    

    and

    string DataAsString1 = encodedText.toStdString();
    
    

    and

    QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
    
    

    and

    QString QString::fromUtf8(const QByteArray &str) 
    

    and other solutions I saw on the web, but the outputs of these options aren't correct. Because after casting, I couldn't convert the encoded text to decoded correctly.

    For example, this is the input string of encoded text: "\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
    and these are the outputs from the different ways I tried so far: \024r�6#���D\033�\020�x�F and \024r�6#���D\033�\020�x�F

    Does anybody suggestion about the right conversion?

    Chris KawaC 1 Reply Last reply
    0
    • J javad_2012

      Hi everyone!
      I want to encrypt the data of a database and to do this, I used AES_128 this link for encryption.

      The result of encryption is a QByteArray. The QByteArray is saved on the text file in the correct shape and I could decode it correctly, but I need to save it on the Sqlite DB. To do this, I should convert it to the QString, save it and do a reverse loop for reading from DB. I tried some options like

      QByteArray encodedText; QString DataAsString = QString(encodedText);
      

      and

      string DataAsString1 = encodedText.toStdString();
      
      

      and

      QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
      
      

      and

      QString QString::fromUtf8(const QByteArray &str) 
      

      and other solutions I saw on the web, but the outputs of these options aren't correct. Because after casting, I couldn't convert the encoded text to decoded correctly.

      For example, this is the input string of encoded text: "\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
      and these are the outputs from the different ways I tried so far: \024r�6#���D\033�\020�x�F and \024r�6#���D\033�\020�x�F

      Does anybody suggestion about the right conversion?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @javad_2012 said:

      I need to save it on the Sqlite DB. To do this, I should convert it to the QString

      That's wrong. After you encode the data it is no longer a string, it's a blob of binary data. Converting it to a QString takes random encrypted data and interprets it as a UTF-16 string. This will result in garbage.

      You've got a blob of binary data, so store it as a blob of binary data, not string. SQLite has a BLOB type. Use that and pass your QByteArray to the query.

      1 Reply Last reply
      2
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by Paul Colby
        #3

        @Chris-Kawa is right, in that you should use SQLite's binary support if possible. However, just for completeness, if you had to convert to QString (eg if you can't control the database's schema), then you should choose some efficient binary-to-text encoding, Base64 being a pretty reasonable option, like:

        const QByteArray encodedTextInBinaryAes128Format = encode(...);
        const QByteArray encodedTextAsBase64Bytes = encodedTextInBinaryAes128Format.toBase64();
        const QString encodedTextAsBase64String = QString::fromLatin1(encodedTextAsBase64Bytes);
        // Now both encodedTextAsBase64Bytes and encodedTextAsBase64String are safe to store as SQLite TEXT values
        // and encodedTextInBinaryAes128Format is safe to store as a SQLite BLOB value (recommended).
        

        Cheers.

        1 Reply Last reply
        2
        • J Offline
          J Offline
          javad_2012
          wrote on last edited by
          #4

          @Chris-Kawa and @PAUL COLBY.
          Thanks alot.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved