Welcome! Log In Create A New Profile


Help needed on converting

Posted by ShaunGVW 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Help needed on converting
November 03, 2009 01:01PM
I am in the process of writing up a Modbus protocol, and slowly I am coming right. However, when I run the program, and read from the socket, what I see while running in debug mode (using breakpoints) and final outout are different. I have the following code...

Language: C++ (QT)
QByteArray block; block = tcpSocket->readAll(); QStringList a; a << QString::number(block[9],16); a << QString::number(block[10],16); a << QString::number(block[11],16); a << QString::number(block[12],16); qDebug() << "Register values :" << a[0] << a[1] << a[2] << a[3];

In debug mode, block[12] shows the value '90', but running the program produces outout of

Register values : "3d" "2b" "52" "ffffffffffffff90"

Can someone explain why I get this output? (...and how to fix it...smile)
Re: Help needed on converting
November 03, 2009 03:22PM
Ok, found out it is because it is larger than 0x80 = 128 in decimal. So how do I read it into the QByteArray as an unsigned char?
Anonymous User
Re: Help needed on converting
November 03, 2009 07:59PM
I haven't looked at the API, but is that not perhaps a result of not flushing/closing the byte array?
Re: Help needed on converting
November 04, 2009 07:49AM
It would seem that it reads it as an signed char, so anything bigger than 128 it takes as being signed, i.e. negative in this case. I am trying to cast it to unsigned using

Language: C++ (QT)
unsigned char* ublock = static_cast<unsigned char*>(block.data());

but get the error

Language: C++ (QT)
invalid static_cast from type `char*'; to type `unsigned char*';
Anonymous User
Re: Help needed on converting
November 04, 2009 09:32AM
I don't think there is such an animal as an unsigned char.
I don't understand QString::number(block[9],16); QString has no static method called number that I can see.

Also, it might be better to initialize by QByteArray block(tcpSocket->readAll()); and call size() on it. Loop through and trying sticking the data into QString?
Re: Help needed on converting
November 04, 2009 07:07PM
[From Qt documentation]

•QString number ( long n, int base = 10 )
•QString number ( double n, char format = 'g', int precision = 6 )
•QString number ( ulong n, int base = 10 )
•QString number ( int n, int base = 10 )
•QString number ( uint n, int base = 10 )
•QString number ( qlonglong n, int base = 10 )
•QString number ( qulonglong n, int base = 10 )

Managed to figure out what to do with the signed char to unsigned char problem. At home now, so can't reproduce it, but it was something with a "data.toHex" in it or similar.
Sorry, only registered users may post in this forum.

Click here to login