PDA

View Full Version : true representation in binary or hex



Tadas
7th October 2010, 20:58
Hi, I have made server which sends true value to device, but I'm looking for solution, how to do this differently. this code works fine

QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << true;
clientConnection->write(ba);

how to change this line:
out << true;
to something like this
out << 01; //this is not working

I'm wondering how can I achieve same result by not using Boolean value true?
Or maybe it is possible to do the same without QDataStream and QByteArray
just do something like this:
clientConnection->write("1") // this is not working

Thanks for advice

wysota
7th October 2010, 21:09
"true" and "1" are two completely different values. The first is boolean and the other is integer, they will be serialized by QDataStream differently. If you want such implicit semantics, don't use QDataStream or make an explicit cast.

Tadas
9th October 2010, 10:09
Hi,

but then what value sends

QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << true;
clientConnection->write(ba);

how simulate boolean value true, how it would look in binary? How to achieve the same result without true?

wysota
9th October 2010, 15:01
Hi,

but then what value sends

QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << true;
clientConnection->write(ba);
Run the program and see for yourself.


how simulate boolean value true,
I don't know what you mean by "simulate" in this context.


how it would look in binary?
true (as a C++ keyword) is usually interpreted as 0x01.


How to achieve the same result without true?
I have no idea what you mean.

Tadas
9th October 2010, 20:43
Tanks wysota for reply,

I have done some testing

QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << true;
qDebug()<<" true value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba[0]"<< ba[0];
qDebug()<<" ba[1]"<< ba[1];
qDebug()<<" ba.toHex()"<< ba.toHex();
qDebug()<<" ba.data()"<< ba.data();

QDataStream out1(&ba, QIODevice::WriteOnly);
out1 << 0x01;
qDebug()<<" 0x01 value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba[0]"<< ba[0];
qDebug()<<" ba[1]"<< ba[1];
qDebug()<<" ba.toHex()"<< ba.toHex();
qDebug()<<" ba.data()"<< ba.data();


Output
true value
size 1
data ""
ba[0] 
ba[1]
ba.toHex() "01"
ba.data() 

0x01 value
size 4
data "
ba[0]
ba[1]
ba.toHex() "00000001"
ba.data()
And I have following questions:
1. Why line qDebug()<<" data"<< ba; do not display any data, how to see ones and zeros. I have used HEX format to see the data.
2. Why 0x01 value in HEX is "00000001" and true is "01" . true has less 0.
3. Why 0x01 value size is 4, why not 8, if in hex it is "00000001" ?
5. if I do out1 << 0x01; I get in HEX "00000001", what value I have to write, if i vant to get answer "01"?

The most iportant for me, is questions 5 and 1. I hope I will understand 2,3,4 after reading some more materials.

Thanks for advice.

SixDegrees
9th October 2010, 21:26
One and zero are not representible characters. There is no screen symbol for them. More importantly, they do NOT correspond to the characters for '0' or '1'; the character codes for those, in ASCII, are 48 and 49 in decimal, or 0x30 and 0x31 in hex.

Also, bits are not bytes, and various base representations of the same number will differ in the number of ASCII characters they display. An integer might take up 4 bytes in memory, for a total of 32 bits, and have a printable representation ranging from one to 16 characters in length, depending on the base used in that representation.

john_god
9th October 2010, 22:33
5. if I do out1 << 0x01; I get in HEX "00000001", what value I have to write, if i vant to get answer "01"?

The most iportant for me, is questions 5 and 1. I hope I will understand 2,3,4 after reading some more materials.

Thanks for advice.

Do something like :

if (out1 == "00000001")
qDebug()<<"01";

squidge
9th October 2010, 23:07
You are using an integer, and the standard size for an integer is 32 bits, which is 8 hex nibbles or 4 bytes.

If you want 1 byte, cast the value to an 8-bit number first. quint8 will do that nicely, me thinks.

Tadas
10th October 2010, 09:03
Thank you all for help.
squidge solution worked. Using quint8 solved my problem:

QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << true;
qDebug()<<" true value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba.toHex()"<< ba.toHex();

QDataStream out1(&ba, QIODevice::WriteOnly);
qint8 a; a=1;
out1 << a;
qDebug()<<" qint8 a value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba.toHex()"<< ba.toHex();
I get the same results with quint8, like if I were using boolean value true:

true value
size 1
data ""
ba.toHex() "01"
qint8 a value
size 1
data ""
ba.toHex() "01"

But If it is possible, can somebody show me how to do this without using quint8, because qint8 is Qt variable, I guess I have to use explicit cast, I want to understand byte programing so 1 working example for my problem, would help me a lot.

I'm reading these articles, but for me it is bit hard to understand them:
http://www.cplusplus.com/doc/tutorial/typecasting/
http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter03_051.html

squidge
10th October 2010, 10:05
Something like:



out1 << (qint8)1;


?

Tadas
10th October 2010, 10:45
Hi, I have finally found something.
Very good tutorial:
http://www.youtube.com/watch?v=d0AwjSpNXR0

And I did this code:

QDataStream out1(&ba, QIODevice::WriteOnly);
unsigned char z=';'; // ; in binary 00111011
z=z>>5; // I shift bits and get 00000001
out1 << z;
qDebug()<<" z value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba.toHex()"<< ba.toHex();

Result

z value
size 1
data ""
ba.toHex() "01"

So now it is the same result if I use car z or boolean value true.

First I did mistake because I used char, but i need to use unsigned char.

Thank you everybody for help.

squidge
10th October 2010, 18:49
Wouldn't it be easier to just use 'unsigned char z=1' instead of using a ';' and the shift?

Tadas
10th October 2010, 19:02
hi,

yes :) it is ...
I always try to do things hard way.:(
but I'm facing now other problems, when I want to send this value over network.
http://www.qtcentre.org/threads/34955-Network-programming-send-binary-values-%28Type-casting%29