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. Raw data conversion using (?) QByteArray

Raw data conversion using (?) QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k Views 3 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.
  • M Offline
    M Offline
    michelson
    wrote on last edited by
    #1

    Hello,
    is there any built-in-qt mechanism to perform such convesion(s)?

    char raw_bytes[4] = {0x00, 0x00, 0xff, 0xff};
    QByteArray byte_array = QByteArray::fromRawData(raw_bytes, 4);
    byte_array.toInt(); // <- i wanted it to return 65535 but result is 0
    
    kshegunovK 1 Reply Last reply
    0
    • M michelson

      Hello,
      is there any built-in-qt mechanism to perform such convesion(s)?

      char raw_bytes[4] = {0x00, 0x00, 0xff, 0xff};
      QByteArray byte_array = QByteArray::fromRawData(raw_bytes, 4);
      byte_array.toInt(); // <- i wanted it to return 65535 but result is 0
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      You're almost there:

      char raw_bytes[4] = {0x00, 0x00, 0xff, 0xff};
      QByteArray byte_array = QByteArray::fromRawData(raw_bytes, 4);
      QDataStream in(&byte_array, QIODevice::ReadOnly);
      
      quint16 value;
      in >> value; // First 2 bytes, gives 0
      in >> value; // Second 2 bytes, 65535
      

      Read and abide by the Qt Code of Conduct

      ? 1 Reply Last reply
      3
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hi!

        char raw_bytes[4] = {0x00, 0x00, 0xff, 0xff};
        QByteArray byte_array = QByteArray::fromRawData(raw_bytes, 4);
        QDataStream stream(byte_array);
        int result;
        stream >> result;
        qDebug() << result; // 65535
        

        Cheers!

        1 Reply Last reply
        4
        • kshegunovK kshegunov

          You're almost there:

          char raw_bytes[4] = {0x00, 0x00, 0xff, 0xff};
          QByteArray byte_array = QByteArray::fromRawData(raw_bytes, 4);
          QDataStream in(&byte_array, QIODevice::ReadOnly);
          
          quint16 value;
          in >> value; // First 2 bytes, gives 0
          in >> value; // Second 2 bytes, 65535
          
          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @kshegunov Ninja!

          1 Reply Last reply
          1
          • M Offline
            M Offline
            michelson
            wrote on last edited by
            #5

            Alright, I think this one can do. Thank you both!

            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