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. How to get non null terminated data out of a QByteArray ?
Forum Updated to NodeBB v4.3 + New Features

How to get non null terminated data out of a QByteArray ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 736 Views 1 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.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on last edited by
    #1

    Hello!

    I'm trying to serialize a struct and send it over the network using QTcpsocket. I've run into a problem where I can't figure out how to extract raw data out of a QBytearray instance.

    Here's a struct defenition:

    typedef struct __attribute__((packed, aligned(4)))
    {
        quint16 checksum1;
        quint16 checksum2;
    
    }MyStruct;
    

    And this is my test case:

        MyStruct sendingStruct;
    
        sendingStruct.checksum1 = 25;
        sendingStruct.checksum2 = 34;
    
        char b[sizeof(sendingStruct)];
        memcpy(b,&sendingStruct,sizeof(sendingStruct)); //b="\031\000"\000" after memcpy (in debugger)
    
        QByteArray ba = QByteArray::fromRawData(b,sizeof(b)); //ba="\031\000"\000" (in debugger)
    
        char *c = ba.data(); // c= "\031" (in debugger)  <= **The problem is here**
    
        MyStruct receivingStruct;
    
        memcpy(&receivingStruct, &c, sizeof(receivingStruct));
    
        qDebug() << QObject::tr("receivingStruct.checksum1 = %1, receivingStruct.checksum2 = %2").arg(receivingStruct.checksum1).arg(receivingStruct.checksum2);
        // output receivingStruct.checksum1 = 65108, receivingStruct.checksum2 = 121
    
    

    How can I get the raw data from QByteArray without it terminating at the first null character it sees?

    VRoninV 1 Reply Last reply
    0
    • C Curtwagner1984

      Hello!

      I'm trying to serialize a struct and send it over the network using QTcpsocket. I've run into a problem where I can't figure out how to extract raw data out of a QBytearray instance.

      Here's a struct defenition:

      typedef struct __attribute__((packed, aligned(4)))
      {
          quint16 checksum1;
          quint16 checksum2;
      
      }MyStruct;
      

      And this is my test case:

          MyStruct sendingStruct;
      
          sendingStruct.checksum1 = 25;
          sendingStruct.checksum2 = 34;
      
          char b[sizeof(sendingStruct)];
          memcpy(b,&sendingStruct,sizeof(sendingStruct)); //b="\031\000"\000" after memcpy (in debugger)
      
          QByteArray ba = QByteArray::fromRawData(b,sizeof(b)); //ba="\031\000"\000" (in debugger)
      
          char *c = ba.data(); // c= "\031" (in debugger)  <= **The problem is here**
      
          MyStruct receivingStruct;
      
          memcpy(&receivingStruct, &c, sizeof(receivingStruct));
      
          qDebug() << QObject::tr("receivingStruct.checksum1 = %1, receivingStruct.checksum2 = %2").arg(receivingStruct.checksum1).arg(receivingStruct.checksum2);
          // output receivingStruct.checksum1 = 65108, receivingStruct.checksum2 = 121
      
      

      How can I get the raw data from QByteArray without it terminating at the first null character it sees?

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

      @Curtwagner1984 said in How to get non null terminated data out of a QByteArray ?:

      <= **The problem is here**

      No it's not, it's here: memcpy(&receivingStruct, &c, sizeof(receivingStruct)); change &c to c or ba.constData(). &c is of type char**

      "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

      C 1 Reply Last reply
      4
      • VRoninV VRonin

        @Curtwagner1984 said in How to get non null terminated data out of a QByteArray ?:

        <= **The problem is here**

        No it's not, it's here: memcpy(&receivingStruct, &c, sizeof(receivingStruct)); change &c to c or ba.constData(). &c is of type char**

        C Offline
        C Offline
        Curtwagner1984
        wrote on last edited by
        #3

        @VRonin Indeed! Thank you!

        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