Important: Please read the Qt Code of Conduct -

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

  • 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;

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

    dataByteArray << packetLength;

    socket->writeDatagram(, inMessage.size(), QHostAddress(""), 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.

  • Please check out the use of "code wrappings":

    I have introduced them for you this time.

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

  • 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.

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

Log in to reply