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. Converting QString to QByteArray using toUtf8() resulting in QByteArray with extra characters
Forum Updated to NodeBB v4.3 + New Features

Converting QString to QByteArray using toUtf8() resulting in QByteArray with extra characters

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 2.7k Views 2 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
    Joshi
    wrote on last edited by
    #1

    I need this to store image in string format and then read the string, convert this string to Qbytearray and load the image back.

    void WorkWithImagesForSerialization_1()
    {
        QImage original_image("someicon.png");
        if (original_image.isNull() != true)
        {
            QByteArray original_bytearray;
            QBuffer original_buffer(&original_bytearray);
            original_buffer.open(QIODevice::WriteOnly);
            original_image.save(&original_buffer, "png"); // writes image into ba in PNG format
    
            QString original_string;
    
            // PMJ : I did this because, constructing QString from byte arry would stop after finding first terminating character
            for (int i=0;i<original_bytearray.size();++i)
            {
                original_string[i] = original_bytearray[i];
            }
    
    
            QByteArray second_bytearray = original_string.toUtf8();
            // original_string =  "\211PNG\r\n\032\n\0.....
            // second_bytearray = "Â\211PNG\r\n\032\n\0....
    
            // Since, image is loaded from byte array, below image second_image is null.
            QImage second_image;
            second_image.loadFromData(second_bytearray);
            // second_image is null here
        }
    }
    

    I expect QByteArray conversion should be proper so that I can reload the image.

    jsulmJ aha_1980A 2 Replies Last reply
    0
    • J Joshi

      I need this to store image in string format and then read the string, convert this string to Qbytearray and load the image back.

      void WorkWithImagesForSerialization_1()
      {
          QImage original_image("someicon.png");
          if (original_image.isNull() != true)
          {
              QByteArray original_bytearray;
              QBuffer original_buffer(&original_bytearray);
              original_buffer.open(QIODevice::WriteOnly);
              original_image.save(&original_buffer, "png"); // writes image into ba in PNG format
      
              QString original_string;
      
              // PMJ : I did this because, constructing QString from byte arry would stop after finding first terminating character
              for (int i=0;i<original_bytearray.size();++i)
              {
                  original_string[i] = original_bytearray[i];
              }
      
      
              QByteArray second_bytearray = original_string.toUtf8();
              // original_string =  "\211PNG\r\n\032\n\0.....
              // second_bytearray = "Â\211PNG\r\n\032\n\0....
      
              // Since, image is loaded from byte array, below image second_image is null.
              QImage second_image;
              second_image.loadFromData(second_bytearray);
              // second_image is null here
          }
      }
      

      I expect QByteArray conversion should be proper so that I can reload the image.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Joshi Why do you need to convert an image to a string? What is the point of doing so? An image will contain any bytes and if you interpret them as UTF8 (!) you will get something like what you get (you can't expect resulting string to be readable).
      Strings are designed to store strings not arbitrary binary data.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        as jlsum mentioned, using QString for storage of binary data is not optimal. In fact I'd stay away from it unless you are doing something like base64 encoding, which is human readable form for binary data used in email attachments and such. There are Qt classes specifically for object serialization. Id suggest researching them instead of shoehorning your binary image data into a QString.

        1 Reply Last reply
        2
        • J Joshi

          I need this to store image in string format and then read the string, convert this string to Qbytearray and load the image back.

          void WorkWithImagesForSerialization_1()
          {
              QImage original_image("someicon.png");
              if (original_image.isNull() != true)
              {
                  QByteArray original_bytearray;
                  QBuffer original_buffer(&original_bytearray);
                  original_buffer.open(QIODevice::WriteOnly);
                  original_image.save(&original_buffer, "png"); // writes image into ba in PNG format
          
                  QString original_string;
          
                  // PMJ : I did this because, constructing QString from byte arry would stop after finding first terminating character
                  for (int i=0;i<original_bytearray.size();++i)
                  {
                      original_string[i] = original_bytearray[i];
                  }
          
          
                  QByteArray second_bytearray = original_string.toUtf8();
                  // original_string =  "\211PNG\r\n\032\n\0.....
                  // second_bytearray = "Â\211PNG\r\n\032\n\0....
          
                  // Since, image is loaded from byte array, below image second_image is null.
                  QImage second_image;
                  second_image.loadFromData(second_bytearray);
                  // second_image is null here
              }
          }
          

          I expect QByteArray conversion should be proper so that I can reload the image.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Joshi As already said by my mates, you cannot store binary data in a QString.

          Just use QByteArray all the way, it is thought for things like that. QString is only for storing (unicode) text.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            To pile upon my fellows, currently QString stores data as UTF-16 so it's not at all suited for your purpose.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • J Offline
              J Offline
              Joshi
              wrote on last edited by
              #6

              @aha_1980 @jsulm @SGaist @Kent-Dorfman
              It all boiled down to store some information in QSettings.
              Reading a QJsonDocument from QSettings posed an issue, however reading QString from QSettings was possible.
              Now, I found out how work with QJsonDocument and QSettings, I no longer need to wrong transfer Image in QString.
              As a work around, I changed Image to Byte Array, changed this Byte Array to hex Array and later changed this Hexed Byte array to QString and stored. But, this is all not required now.
              Thanks for your inputs.

              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