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 Union to QByteArray and vice-versa using QUdpSocket in Qt
Qt 6.11 is out! See what's new in the release blog

Convert Union to QByteArray and vice-versa using QUdpSocket in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 381 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.
  • D Offline
    D Offline
    devillIsHeree
    wrote on last edited by
    #1

    I have one Union like below

    #define no_of_bits 240
    #define word_size 16
    
    struct bits
    {
        unsigned short int bit1:1;
        unsigned short int bit2:1;
        unsigned short int bit3:1;
        unsigned short int bit4:1;
        unsigned short int bit5:1;
        unsigned short int bit6:1;
        unsigned short int bit7:1;
        unsigned short int bit8:1;
        unsigned short int bit9:1;
        unsigned short int bit10:1;
        unsigned short int bit11:1;
        unsigned short int bit12:1;
        unsigned short int bit13:1;
        unsigned short int bit14:1;
        unsigned short int bit15:1;
        unsigned short int bit16:1;
    };
    
    union myData
    {
            unsigned short int data[no_of_bits/word_size];
            struct bits word[no_of_bits/word_size];
    };
    

    I want to convert myData union into a QByteArray so that I can send it through socket using QUdpSocket using writeDatagram() function. So first tell me how can I convert this into QByteArray.

    Next thing is that, On the receiver side how can I convert the QByteArray into the this Union myData again.
    In short I want to convert this union myData into QByteArray so that I can send this over a network using QUdpSocket and on the receiver side reconvert this received QByteArray into the Union again.

    JonBJ 1 Reply Last reply
    0
    • D devillIsHeree

      I have one Union like below

      #define no_of_bits 240
      #define word_size 16
      
      struct bits
      {
          unsigned short int bit1:1;
          unsigned short int bit2:1;
          unsigned short int bit3:1;
          unsigned short int bit4:1;
          unsigned short int bit5:1;
          unsigned short int bit6:1;
          unsigned short int bit7:1;
          unsigned short int bit8:1;
          unsigned short int bit9:1;
          unsigned short int bit10:1;
          unsigned short int bit11:1;
          unsigned short int bit12:1;
          unsigned short int bit13:1;
          unsigned short int bit14:1;
          unsigned short int bit15:1;
          unsigned short int bit16:1;
      };
      
      union myData
      {
              unsigned short int data[no_of_bits/word_size];
              struct bits word[no_of_bits/word_size];
      };
      

      I want to convert myData union into a QByteArray so that I can send it through socket using QUdpSocket using writeDatagram() function. So first tell me how can I convert this into QByteArray.

      Next thing is that, On the receiver side how can I convert the QByteArray into the this Union myData again.
      In short I want to convert this union myData into QByteArray so that I can send this over a network using QUdpSocket and on the receiver side reconvert this received QByteArray into the Union again.

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

      @devillIsHeree
      At the end of the day, your struct/union is a bunch of bytes. https://doc.qt.io/qt-5/qbytearray.html#QByteArray-1 and https://doc.qt.io/qt-5/qbytearray.html#constData let you swap between QByteArray and const char *. The rest is just casting. I am assuming both sides have same architecture, compilers etc.; if not, that is a separate matter, but you don't ask about that,

      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