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] Using Unsigned Char to store data from 0 - 255.
Forum Update on Monday, May 27th 2025

[SOLVED] Using Unsigned Char to store data from 0 - 255.

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

    I am working on a system that has very low processing power and memory so trying to use unsigned chars to store values between 0 and 255. I need to capture a number using an unsigned char, send through a UDPSocket and then read the value on the other side. I have read numerous other posts and can’t find what I am doing wrong. Here is my code.
    @
    Unsigned char packetLength = ‘32’;
    QByteArray dataByteArray;
    dataByteArray.append(packetLength);

    QDataStream dataStream(&dataByteArray, QIODevice::ReadWrite);

    dataByteArray << packetLength;

    socket->writeDatagram(dataByteArray.data(), inMessage.size(), QHostAddress("101.1.1.103"), 1234);
    @
    It connects and transmits fine so on the other side I have this:
    @
    QDataStream qstr(&inPackets, QIODevice::ReadWrite);
    Unsigned char packetL;
    qstr >> packetL;
    qDebug() >> packetL;
    @
    This prints a value of packet = 210. I am confused and think this should be easy. Any help is appreciated on how I can get the value 32 across.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Please check out the use of "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01

      I have introduced them for you this time.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gundi
        wrote on last edited by
        #3

        Koahnig thank you. I just checked it out and will use it going forward.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marsellus
          wrote on last edited by
          #4

          Hi,

          The packet length you send is wrong.
          You should replace the first line with the following:
          @unsigned char packetLength = 32;@

          This way, you store an integer (whose value is 32) in an unsigned char.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gundi
            wrote on last edited by
            #5

            That worked. I knew it had to be something simple. Much appreciated.

            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