Results 1 to 5 of 5

Thread: Passing Specific Binary Characters in QTcpSocket.

  1. #1
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Passing Specific Binary Characters in QTcpSocket.

    I have this to write some data to a socket.

    Qt Code:
    1. QDataStream out(&data, QIODevice::WriteOnly);
    2. out.setVersion(QDataStream::Qt_4_1);
    3. out << int(0x02) << QString("text");
    4. tcpSocket->write(data);
    To copy to clipboard, switch view to plain text mode 
    The Socket connects perfectly, and the data is sent however the results seen at the receiving end are not what i expected... I know there is a simple fundamental problem with the way I am defining the data to send.. i just cant see it..

    I want to see this on the other end. It can only be this since this is a tool to integrate to an existing protocol.


    0x02 74 65 78 74 0A

    Which is [0x02]text[0x0A]


    But I keep getting this..

    0x00 00 00 02 00 00 00 08 00 74 00 65 00 78 00 74

    [0x00 00 00 02 00 00 00 08 00]t[0x00]e[0x00]x[0x00]t

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing Specific Binary Characters in QTcpSocket.

    That is because you write unicode characters to the data stream.
    What you need is to write ascii characters:
    Qt Code:
    1. QString s = "text";
    2. QByteArray b = s.toAscii();
    3. const char* ascii = b.constData();
    4. out << int(0x02) << ascii;
    To copy to clipboard, switch view to plain text mode 


    Regards

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

    nbkhwjm (2nd September 2007)

  4. #3
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Passing Specific Binary Characters in QTcpSocket.

    That did indeed fix the double byte characters in the QString.. however i still have many additional bytes where on the 0x02 should be...

    So, i made a test..

    with this code:
    Qt Code:
    1. QDataStream out(&data, QIODevice::WriteOnly);
    2. out.setVersion(QDataStream::Qt_4_1);
    3. out << int(0x02);
    4. tcpSocket->write(data);
    To copy to clipboard, switch view to plain text mode 

    I get this on the network..
    0x00000002

    What I need to see is :
    0x02

    Is there a different way to define this instead of int(0x02) to make do only this hex character? and not the additional nulls?

    Thanks for the assistance...

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing Specific Binary Characters in QTcpSocket.

    But you want to see 0x02(8 bits - 1 byte) and still you write an integer(32 bits - 4bytes).
    Write a qint8 instead:
    Qt Code:
    1. out << qint8(0x02);
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. The following user says thank you to marcel for this useful post:

    nbkhwjm (2nd September 2007)

  7. #5
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Passing Specific Binary Characters in QTcpSocket.

    Thanks marcel,

    yes, that was a fundamental mistake... works great now..

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

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.