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. QDataStream won't work with custom crafted char array
Forum Updated to NodeBB v4.3 + New Features

QDataStream won't work with custom crafted char array

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.1k 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
    Davita
    wrote on last edited by
    #1

    Hi guys. I have an application which consists of two primary modules. One is written in C, uses standard C runtime library and one written in Qt C++. They communicate with each other with IPC. C module creates a char array, fills it with data and sends to the module written in Qt. I want to deserialize received data using QDataStream, but my efforts didn't yield any result yet. Here's a simple example what I'm trying to achieve:

    @ unsigned int pointer = 0;
    const int IPC_MSG_LEN = 500;
    const int IPC_MSG_HEADER = 200;
    const int SOMETHING = 1443;
    char api = 55;

    char msg[IPC_MSG_LEN] = {0};
    memcpy_s(msg, IPC_MSG_LEN, &IPC_MSG_HEADER, sizeof(int));
    pointer = sizeof(unsigned int);
    memcpy_s(&msg[pointer], IPC_MSG_LEN - pointer, &api, sizeof(char));
    ++pointer;
    memcpy_s(&msg[pointer], IPC_MSG_LEN - pointer, &SOMETHING, sizeof(int));
    
    QByteArray arr(msg, IPC_MSG_LEN);
    QDataStream ds(&arr, QIODevice::ReadOnly);
    qint32 header = 0, aa = 0;
    qint8 t_api = 0;
    ds >> header; //Doesn't work
    ds >> t_api; //Works
    ds >> aa; //Doesn't work
    

    @

    As you can see, the code is pretty simple, but header and aa variables are deserialized to a random number. However t_api (one byte variable) has correct value assigned.
    So what's the problem with this code? Does QDataStream uses a private data format which is not compatible with the one I'm using? Should I write my own QIODevice implementation or there is a quick fix I'm not aware of? :)
    Thanks, I appreciate your help.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      It's most probably a byte order issue. Windows uses little endian, whereas QDataStream by default uses big endian. You should add this right after the data stream instantiation (after line 15):

      @
      ds.setByteOrder(QDataStream::LittleEndian);
      @

      A platform independent way would be:

      @
      #include <QSysInfo>

      ds.setByteOrder(QSysInfo::ByteOrder);
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      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