PDA

View Full Version : Network programming send binary values (Type casting)



Tadas
10th October 2010, 18:28
Hi,

Is it possible to send unsigned char value over network without using QDataStream and QByteArray

...................
unsigned char z=';';
z=z>>5;
clientConnection->write(z);
I get error

error: invalid conversion from 'unsigned char' to 'const char*'

if I do like this

unsigned char z=';';
z=z>>5;
QByteArray ba;
QDataStream out(&ba, QIODevice::WriteOnly);
out << z;
clientConnection->write(z);
It works, But I'm interested, how to send value z without using QByteArray and QDataStream. If it is possible.

I think I have to use type casting, but how ? Type casting topic is not easy to understand for newbies:
http://www.cplusplus.com/doc/tutorial/typecasting/

squidge
10th October 2010, 18:43
Did you try clientConnection->write((const char *)&z); ?

That will get past the type casting, but where do you specify the length? There's no way to tell using a standard 'const char *' type, unless you rely on null termination of course.

[So the above will most likely crash or give undefined results]

tbscope
10th October 2010, 18:45
Type casting topic is not easy to understand for newbies:
A lucky coincidence because if it is that hard to understand it is maybe a good idea to leave it alone and only use it when everything else fails.

Why do you want to use casts?

Tadas
10th October 2010, 19:20
Thank you squidge,

I had to send 0x01 so I did this
clientConnection->write((const char *)&z, 2);
And it worked like I needed :)
I think in this case number 2 represents, that 2 bits of value z will be sent.


A lucky coincidence because if it is that hard to understand it is maybe a good idea to leave it alone and only use it when everything else fails.
Yes it is my bad habit to learn hard things, (without this answer it would be hard for me to got to sleep :)), but now I have some example, with which I will be able to do some experiments.

Maybe I'm wrong, but type casting is important thing to learn, because it makes life easier. Maybe I'm wrong :)

Thanks for help

tbscope
10th October 2010, 19:42
I had to send 0x01 so I did this
clientConnection->write((const char *)&z, 2);
And it worked like I needed :)
I think in this case number 2 represents, that 2 bits of value z will be sent.

No, that is not correct. The write function writes bytes, not bits. You have set the maximum length to 2 bytes.


Yes it is my bad habit to learn hard things, (without this answer it would be hard for me to got to sleep :))
That is something I understand and something I think is admirable.


Maybe I'm wrong, but type casting is important thing to learn, because it makes life easier. Maybe I'm wrong :)
It's always a good idea to have as much knowledge as possible about something. That includes learning how to cast. However, it is not always needed to use it when something else already provides you with a way that is less subjective to errors. The use of standard Qt classes is preferred over doing it yourself.

wysota
10th October 2010, 21:53
I had to send 0x01

Hmm...


QByteArray ba;
ba.append((char)0x01);
socket->write(ba);

ChrisW67
10th October 2010, 23:15
I had to send 0x01 so I did this
clientConnection->write((const char *)&z, 2);
And it worked like I needed

I suspect that it didn't. You told write to take two bytes from the location occupied by the single byte in z. The second byte will be whatever random data was in the byte following z, which may be a padding byte, another bit of your data, a code byte, or random junk left by some other process.