Results 1 to 7 of 7

Thread: Network programming send binary values (Type casting)

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Network programming send binary values (Type casting)

    Hi,

    Is it possible to send unsigned char value over network without using QDataStream and QByteArray
    Qt Code:
    1. ...................
    2. unsigned char z=';';
    3. z=z>>5;
    4. clientConnection->write(z);
    To copy to clipboard, switch view to plain text mode 
    I get error
    error: invalid conversion from 'unsigned char' to 'const char*'
    if I do like this
    Qt Code:
    1. unsigned char z=';';
    2. z=z>>5;
    3. QDataStream out(&ba, QIODevice::WriteOnly);
    4. out << z;
    5. clientConnection->write(z);
    To copy to clipboard, switch view to plain text mode 
    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/

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Network programming send binary values (Type casting)

    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]

  3. The following user says thank you to squidge for this useful post:

    Tadas (10th October 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Network programming send binary values (Type casting)

    Quote Originally Posted by Tadas View Post
    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?

  5. #4
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Network programming send binary values (Type casting)

    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

  6. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Network programming send binary values (Type casting)

    Quote Originally Posted by Tadas View Post
    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.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Network programming send binary values (Type casting)

    Quote Originally Posted by Tadas View Post
    I had to send 0x01
    Hmm...

    Qt Code:
    1. ba.append((char)0x01);
    2. socket->write(ba);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,641
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Network programming send binary values (Type casting)

    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.

Similar Threads

  1. Problem with type casting
    By qtDave in forum Newbie
    Replies: 7
    Last Post: 9th November 2009, 20:57
  2. Problem regaridng Type casting QVariant
    By sudheer168 in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 06:02
  3. Problem with type casting?
    By Jyoti.verma in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2009, 05:24
  4. Send QImage over network
    By winston2020 in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2009, 19:09
  5. Send QEvents over network?
    By Winni in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2007, 20:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.