Results 1 to 3 of 3

Thread: how to get 5 as int from number=QByteArray::fromHex("35");

  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 how to get 5 as int from number=QByteArray::fromHex("35");

    Hi,

    how to get 5 as int from number=QByteArray::fromHex("05");
    or how to add 30 as hex to hex value 05, becouse
    QByteArray::fromHex("35").toInt(); will give me int as 5

    another question
    Qt Code:
    1. QByteArray number2;
    2. int nubint=3345;
    3. number2= QByteArray::number(nubint).length();
    4. qDebug()<<"number2"<<number2.toHex();
    To copy to clipboard, switch view to plain text mode 
    why do I get number2 in hex as 04 in hex, but not as 34 ;

    and why i get error message if I will edit 3 line like this number2= QByteArray::number(nubint).length();
    Qt Code:
    1. error: invalid conversion from 'int' to 'const char*'
    2. error: initializing argument 1 of 'QByteArray& QByteArray::operator=(const char*)'
    To copy to clipboard, switch view to plain text mode 

    i'm totally messed up with bits and bytes..

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: how to get 5 as int from number=QByteArray::fromHex("35");

    I don't know, but there must be a reason Nokia's Wiki tells that this is done using QString, not QBytearray:
    http://www.developer.nokia.com/Commu...ce-versa_in_Qt

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to get 5 as int from number=QByteArray::fromHex("35");

    Qt Code:
    1. QByteArray ba = QByteArray::fromHex("05");
    2. int number = ba.at(0);
    3. qDebug() << number;
    To copy to clipboard, switch view to plain text mode 
    number is 5, exactly as you asked for. You are clearly confused between the number 5 and the character '5'. The binary value used to represent these things are not the same (5 and 53 respectively). Take a step back from the solution and explain the problem you are trying to solve.

    QByteArray::number() returns a sequence of bytes representing the number in ASCII. The length of that sequence is 4 ("04" in hex).

    QByteArray::length() returns an integer and you are trying to store that value into a QByteArray. This is clearly not a perfect match, but the compiler tries to find a way to automatically convert an int to a QByteArray through a conversion constructor, i.e. something like QByteArray::QByteArray(int value), fails and reports the error. The specifics of the error message are because the closest conversion constructor available expects a "const char*" and it cannot convert "int" to that type.

Similar Threads

  1. Replies: 3
    Last Post: 29th April 2010, 19:11
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. QPlainTextEdit - "real" column number
    By Carlsberg in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2009, 11:53
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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
  •  
Qt is a trademark of The Qt Company.