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 uint8_t data to a format that QByteArray can use?
Forum Updated to NodeBB v4.3 + New Features

Converting uint8_t data to a format that QByteArray can use?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 7.5k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by SGaist
    #2

    Hi,

    You can use a reinterpret_cast. You should also give the size parameter to ensure you have the right amount of data copied.

    [edit: fixed cast type SGaist]

    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
    4
    • D Daniel Williams

      I'm working with the pleora sdk and I receive the image data in a proprietary structure (PvBuffer). But, they have a couple of methods to help convert the data which I get back as a unint8_t *.

      My ultimate goal is to insert the data into a SQLite database. The example I'm following, puts the data in a QByteArray. then performs the insert.

      I'm not sure how to convert the data from uint8_t * to a QByteArray.

      PvBuffer dataFromCamera = getMyData();
      PvImage img = dataFromCamera.getImage();
      uint8_t *rawData = img.GetDataPointer();
      
      //Everything is good to this point.
      
      unsigned char *c = static_cast<unsigned char *>(rawData);  //This is ok.
      
      QByteArray ba(c);  
      //This line gives an error:  no matching constructyor for intialization of 'QByteArray'
      //No known conversion from unsigned char * to const char *.
      

      Is there another way to instantiate a QByteArray with the data?

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

      @Daniel-Williams

      uint8_t is unsigned char, so your cast does nothing at the moment.

      QByteArray in contrast, uses char, which is signed for most compilers. Also, I'm not sure if static_cast works for pointers, I think you need reinterpret_cast.

      The following should work (of course all lines can be written as one, but so it's better understandable):

      const uint8_t *rawData = img.GetDataPointer();
      
      const char *c = reinterpret_cast<const char *>(rawData);
      QByteArray ba(c);
      

      Regards

      Qt has to stay free or it will die.

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

        @aha_1980 is correct, I wrote down the wrong cast type. Fixed it.

        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
        0
        • aha_1980A aha_1980

          @Daniel-Williams

          uint8_t is unsigned char, so your cast does nothing at the moment.

          QByteArray in contrast, uses char, which is signed for most compilers. Also, I'm not sure if static_cast works for pointers, I think you need reinterpret_cast.

          The following should work (of course all lines can be written as one, but so it's better understandable):

          const uint8_t *rawData = img.GetDataPointer();
          
          const char *c = reinterpret_cast<const char *>(rawData);
          QByteArray ba(c);
          

          Regards

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

          @aha_1980 said in Converting uint8_t data to a format that QByteArray can use?:

          uint8_t is unsigned char, so your cast does nothing at the moment

          There are machines where a char consists of more than one byte though

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

          VRoninV 1 Reply Last reply
          1
          • D Daniel Williams

            I'm working with the pleora sdk and I receive the image data in a proprietary structure (PvBuffer). But, they have a couple of methods to help convert the data which I get back as a unint8_t *.

            My ultimate goal is to insert the data into a SQLite database. The example I'm following, puts the data in a QByteArray. then performs the insert.

            I'm not sure how to convert the data from uint8_t * to a QByteArray.

            PvBuffer dataFromCamera = getMyData();
            PvImage img = dataFromCamera.getImage();
            uint8_t *rawData = img.GetDataPointer();
            
            //Everything is good to this point.
            
            unsigned char *c = static_cast<unsigned char *>(rawData);  //This is ok.
            
            QByteArray ba(c);  
            //This line gives an error:  no matching constructyor for intialization of 'QByteArray'
            //No known conversion from unsigned char * to const char *.
            

            Is there another way to instantiate a QByteArray with the data?

            Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #6

            @Daniel-Williams said in Converting uint8_t data to a format that QByteArray can use?:

            I'm working with the pleora sdk and I receive the image data in a proprietary structure (PvBuffer). But, they have a couple of methods to help convert the data which I get back as a unint8_t *.

            My ultimate goal is to insert the data into a SQLite database. The example I'm following, puts the data in a QByteArray. then performs the insert.

            I'm not sure how to convert the data from uint8_t * to a QByteArray.

            PvBuffer dataFromCamera = getMyData();
            PvImage img = dataFromCamera.getImage();
            uint8_t *rawData = img.GetDataPointer();
            
            //Everything is good to this point.
            
            unsigned char *c = static_cast<unsigned char *>(rawData);  //This is ok.
            
            QByteArray ba(c);  
            //This line gives an error:  no matching constructyor for intialization of 'QByteArray'
            //No known conversion from unsigned char * to const char *.
            

            Is there another way to instantiate a QByteArray with the data?

            the type casting is something you'll figure out. the more important thing you need to consider is that you DONT WANT to assume data returned from img.GetDataPointer() is a zero terminated c_str(). you better use a constructor with an explicit length value.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @aha_1980 said in Converting uint8_t data to a format that QByteArray can use?:

              uint8_t is unsigned char, so your cast does nothing at the moment

              There are machines where a char consists of more than one byte though

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #7

              @jsulm said in Converting uint8_t data to a format that QByteArray can use?:

              There are machines where a char consists of more than one byte though

              Nope, char (both signed and unsigned) is the only type that is required by the standard to have sizeof() equal to 1. Your architecture might use number of bits in a byte != 8 but char must always be 1 byte

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              jsulmJ 1 Reply Last reply
              1
              • VRoninV VRonin

                @jsulm said in Converting uint8_t data to a format that QByteArray can use?:

                There are machines where a char consists of more than one byte though

                Nope, char (both signed and unsigned) is the only type that is required by the standard to have sizeof() equal to 1. Your architecture might use number of bits in a byte != 8 but char must always be 1 byte

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

                @VRonin That's correct. But if my machine defines a byte as 16bit what would uint8_t be then?

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

                VRoninV JonBJ 2 Replies Last reply
                1
                • jsulmJ jsulm

                  @VRonin That's correct. But if my machine defines a byte as 16bit what would uint8_t be then?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #9

                  @jsulm said in Converting uint8_t data to a format that QByteArray can use?:

                  But if my machine defines a byte as 16bit what would uint8_t be then?

                  Never programmed GPU stuff so not sure how it's defined but would be very interesting if someone with a non-8-bits system could test it

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @VRonin That's correct. But if my machine defines a byte as 16bit what would uint8_t be then?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

                    @jsulm

                    @VRonin That's correct. But if my machine defines a byte as 16bit what would uint8_t be then?

                    https://stackoverflow.com/questions/13615764/is-a-byte-always-8-bits

                    In modern terms there are always 8 bits to a byte. This has been since 1993 IIRC. ISO/IEC 2382-1:1993.
                    At the end of the day I think it would be rare to find a machine today that was not based on 8 bits to a byte.

                    Yes, a byte is always 8 bits in modern computing.

                    Are you really sure you have a 16-bit byte (not word)?? :)

                    VRoninV 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jsulm

                      @VRonin That's correct. But if my machine defines a byte as 16bit what would uint8_t be then?

                      https://stackoverflow.com/questions/13615764/is-a-byte-always-8-bits

                      In modern terms there are always 8 bits to a byte. This has been since 1993 IIRC. ISO/IEC 2382-1:1993.
                      At the end of the day I think it would be rare to find a machine today that was not based on 8 bits to a byte.

                      Yes, a byte is always 8 bits in modern computing.

                      Are you really sure you have a 16-bit byte (not word)?? :)

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

                      @JonB said in Converting uint8_t data to a format that QByteArray can use?:

                      Are you really sure you have a 16-bit byte (not word)??

                      Windows CE has 16bits as a minimum byte size AFAIK

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      JonBJ 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        @JonB said in Converting uint8_t data to a format that QByteArray can use?:

                        Are you really sure you have a 16-bit byte (not word)??

                        Windows CE has 16bits as a minimum byte size AFAIK

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #12

                        @VRonin
                        I found that real surprising; you would have thought Googling for windows ce byte size would readily throw this up, but it does not. However, buried away I happenstanced across https://stackoverflow.com/a/5516161/489865

                        Windows CE does roughly the same: its smallest type (at least with Microsoft's compiler) is 16 bits. They do not, however, treat a char as 16 bits -- instead they take the (non-conforming) approach of simply not supporting a type named char at all.

                        So you are right! However, given that then they apparently do not support char I would have thought that itself would create headaches...! :)

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Daniel Williams
                          wrote on last edited by
                          #13

                          @aha_1980 @SGaist You nailed it. The reinterpret_cast worked. Thank you!

                          1 Reply Last reply
                          1

                          • Login

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