Results 1 to 13 of 13

Thread: true representation in binary or hex

  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 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
    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3. clientConnection->write(ba);
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by Tadas; 7th October 2010 at 21:18.

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

    Default 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.
    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.


  3. #3
    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: true representation in binary or hex

    Hi,

    but then what value sends
    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3. clientConnection->write(ba);
    To copy to clipboard, switch view to plain text mode 

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

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

    Default Re: true representation in binary or hex

    Quote Originally Posted by Tadas View Post
    Hi,

    but then what value sends
    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3. clientConnection->write(ba);
    To copy to clipboard, switch view to plain text mode 
    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.
    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.


  5. #5
    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: true representation in binary or hex

    Tanks wysota for reply,

    I have done some testing
    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3. qDebug()<<" true value";
    4. qDebug()<<" size"<< ba.size();
    5. qDebug()<<" data"<< ba;
    6. qDebug()<<" ba[0]"<< ba[0];
    7. qDebug()<<" ba[1]"<< ba[1];
    8. qDebug()<<" ba.toHex()"<< ba.toHex();
    9. qDebug()<<" ba.data()"<< ba.data();
    10.  
    11. QDataStream out1(&ba, QIODevice::WriteOnly);
    12. out1 << 0x01;
    13. qDebug()<<" 0x01 value";
    14. qDebug()<<" size"<< ba.size();
    15. qDebug()<<" data"<< ba;
    16. qDebug()<<" ba[0]"<< ba[0];
    17. qDebug()<<" ba[1]"<< ba[1];
    18. qDebug()<<" ba.toHex()"<< ba.toHex();
    19. qDebug()<<" ba.data()"<< ba.data();
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  7. #7
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: true representation in binary or hex

    Quote Originally Posted by Tadas View Post
    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 :
    Qt Code:
    1. if (out1 == "00000001")
    2. qDebug()<<"01";
    To copy to clipboard, switch view to plain text mode 
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

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

    Default 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.

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

    Tadas (10th October 2010)

  10. #9
    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: true representation in binary or hex

    Thank you all for help.
    squidge solution worked. Using quint8 solved my problem:
    Qt Code:
    1. QDataStream out(&ba, QIODevice::WriteOnly);
    2. out << true;
    3. qDebug()<<" true value";
    4. qDebug()<<" size"<< ba.size();
    5. qDebug()<<" data"<< ba;
    6. qDebug()<<" ba.toHex()"<< ba.toHex();
    7.  
    8. QDataStream out1(&ba, QIODevice::WriteOnly);
    9. qint8 a; a=1;
    10. out1 << a;
    11. qDebug()<<" qint8 a value";
    12. qDebug()<<" size"<< ba.size();
    13. qDebug()<<" data"<< ba;
    14. qDebug()<<" ba.toHex()"<< ba.toHex();
    To copy to clipboard, switch view to plain text mode 
    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_boo...ter03_051.html
    Last edited by Tadas; 10th October 2010 at 09:07.

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

    Default Re: true representation in binary or hex

    Something like:

    Qt Code:
    1. out1 << (qint8)1;
    To copy to clipboard, switch view to plain text mode 

    ?

  12. #11
    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: 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:
    Qt Code:
    1. QDataStream out1(&ba, QIODevice::WriteOnly);
    2. unsigned char z=';'; // ; in binary 00111011
    3. z=z>>5; // I shift bits and get 00000001
    4. out1 << z;
    5. qDebug()<<" z value";
    6. qDebug()<<" size"<< ba.size();
    7. qDebug()<<" data"<< ba;
    8. qDebug()<<" ba.toHex()"<< ba.toHex();
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Tadas; 10th October 2010 at 10:53.

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

    Default 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?

  14. #13
    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: 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
    Last edited by Tadas; 10th October 2010 at 19:06.

Similar Threads

  1. Graphical representation of an array
    By ithinkso in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 23:30
  2. QTabWidget and .setTabsClosable(True)
    By prof.ebral in forum Newbie
    Replies: 3
    Last Post: 26th February 2010, 04:36
  3. Tree related representation
    By aegis in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2007, 12:20
  4. Replies: 1
    Last Post: 2nd April 2007, 22:52
  5. Replies: 12
    Last Post: 7th February 2007, 13:30

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.