[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;
    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.


  • Moderators

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

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.