true representation in binary or hex
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
Code:
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
Re: true representation in binary or hex
"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.
Re: true representation in binary or hex
Hi,
but then what value sends
Code:
out << true;
clientConnection->write(ba);
how simulate boolean value true, how it would look in binary? How to achieve the same result without true?
Re: true representation in binary or hex
Quote:
Originally Posted by
Tadas
Hi,
but then what value sends
Code:
out << true;
clientConnection->write(ba);
Run the program and see for yourself.
Quote:
how simulate boolean value true,
I don't know what you mean by "simulate" in this context.
Quote:
how it would look in binary?
true (as a C++ keyword) is usually interpreted as 0x01.
Quote:
How to achieve the same result without true?
I have no idea what you mean.
Re: true representation in binary or hex
Tanks wysota for reply,
I have done some testing
Code:
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();
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
Quote:
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.
Re: true representation in binary or hex
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.
Re: true representation in binary or hex
Quote:
Originally Posted by
Tadas
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 :
Code:
if (out1 == "00000001")
qDebug()<<"01";
Re: true representation in binary or hex
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.
Re: true representation in binary or hex
Thank you all for help.
squidge solution worked. Using quint8 solved my problem:
Code:
out << true;
qDebug()<<" true value";
qDebug()<<" size"<< ba.size();
qDebug()<<" data"<< ba;
qDebug()<<" ba.toHex()"<< ba.toHex();
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:
Quote:
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_boo...ter03_051.html
Re: true representation in binary or hex
Re: true representation in binary or hex
Hi, I have finally found something.
Very good tutorial:
http://www.youtube.com/watch?v=d0AwjSpNXR0
And I did this code:
Code:
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
Quote:
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.
Re: true representation in binary or hex
Wouldn't it be easier to just use 'unsigned char z=1' instead of using a ';' and the shift?
Re: true representation in binary or hex
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/3495...ype-casting%29