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. [SOLVED] reading 16 bit integer using QByteArray
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] reading 16 bit integer using QByteArray

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k 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.
  • H Offline
    H Offline
    habbas33
    wrote on 12 Sept 2014, 06:43 last edited by
    #1

    hi... I want to read a file with QByteArray but the problem is that it read byte wise and i want array of 16 bit integer. here is my code...

    @
    QByteArray fileBuf;
    sprintf_s(filepath, "c:/file.bin");}
    myfile.setFileName(filepath);
    if(!myfile.open(QIODevice::ReadOnly)) return;
    fileBuf=myfile.readAll();@

    here is the test to find values inside

    @ qint16 z;
    for(int test =0; test < 5 ; test++)
    {
    z= (fileBuf[i]);
    qDebug()<< i<<"is="<<z;@

    result:
    0 is= -88 (// in binary// 1111 1111 1010 1000)
    1 is= -2 (// in binary// 1111 1111 1111 1110)
    2 is= -64
    3 is= -3
    4 is= 52

    these are because of 8 bit array i need 16 bit i,e.. at value at 0 = -344 (//binary// 1111 11110 1010 1000)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 12 Sept 2014, 06:54 last edited by
      #2

      I suggest to use "QDataStream":http://qt-project.org/doc/qt-5/qdatastream.html to read directly from file or reading all file contents and use "QBuffer":http://qt-project.org/doc/qt-5/QBuffer.html

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        habbas33
        wrote on 12 Sept 2014, 12:12 last edited by
        #3

        thanks for your response it worked now...

        QFile myfile;
        myfile.setFileName("c:/file.bin");
        if(!myfile.open(QIODevice::ReadOnly)) return;

        QDataStream data(&myfile);
        data.setByteOrder(QDataStream::LittleEndian);
        QVector<qint16> result;
        while(!data.atEnd()) {
        qint16 x;
        data >> x;
        result.append(x);
        }

        1 Reply Last reply
        0

        1/3

        12 Sept 2014, 06:43

        • Login

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