Results 1 to 13 of 13

Thread: Convert QbyteArray to int return 0 (problem)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Convert QbyteArray to int return 0 (problem)

    Hello,

    I convert a QByteArray to int using the toInt() function
    So when I try to display the int value, its return 0
    I'm trying also toUInt()

    Qt Code:
    1. QByteArray* ByteID = new QByteArray;
    2. ByteID = oneFrame.mid(3,1);
    3. qDebug() << "ByteID = " << (*ByteID).toHex(); // return 0A --> working perfectly
    4. qDebug() << "ByteID = " << (*ByteID).toUInt(); // return 0
    To copy to clipboard, switch view to plain text mode 

    I'm serching on Google but I don't find a correct response

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Convert QbyteArray to int return 0 (problem)

    Better read documentation than google
    Qt Code:
    1. // example from QByteArray::toInt docs:
    2. QByteArray str("FF");
    3. bool ok;
    4. int hex = str.toInt(&ok, 16); // hex == 255, ok == true
    5. int dec = str.toInt(&ok, 10); // dec == 0, ok == false
    To copy to clipboard, switch view to plain text mode 

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

    AUDI_ENG (8th July 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert QbyteArray to int return 0 (problem)

    Quote Originally Posted by stampede View Post
    Better read documentation than google
    Qt Code:
    1. // example from QByteArray::toInt docs:
    2. QByteArray str("FF");
    3. bool ok;
    4. int hex = str.toInt(&ok, 16); // hex == 255, ok == true
    5. int dec = str.toInt(&ok, 10); // dec == 0, ok == false
    To copy to clipboard, switch view to plain text mode 
    I readed documentation , I'm trying this code, nothing changed, I have 0 as result (for dec)


    nb data read = 11 Data readed = "aaaa0c0e09090a0809090a"
    Qt Code:
    1. *ByteID = oneFrame.mid(2,1);
    2. int dec = (*ByteID).toInt(&ok,16);
    To copy to clipboard, switch view to plain text mode 
    ByteID_hex = "0c"
    dec = 0
    Last edited by AUDI_ENG; 8th July 2014 at 10:38.

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Convert QbyteArray to int return 0 (problem)

    Hi,

    "FF" is not a valid value in decimal representation but is a valid value in hexadecimal. This is what "ok" variable is telling you.
    Òscar Llarch i Galán

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

    AUDI_ENG (8th July 2014)

  7. #5
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert QbyteArray to int return 0 (problem)

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    "FF" is not a valid value in decimal representation but is a valid value in hexadecimal. This is what "ok" variable is telling you.
    Hi,
    Ok thank you, So How I can convert the QbyteArray to a valid decimal value ?

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert QbyteArray to int return 0 (problem)

    But QByteArray::toInt works with "string equivalent of the number". 0x0C is NOT valid ASCII digit.

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

    AUDI_ENG (8th July 2014)

  10. #7
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert QbyteArray to int return 0 (problem)

    Thank you for this clear response
    So, Qt has another function to convert a QByteArray to Int?
    Last edited by AUDI_ENG; 8th July 2014 at 12:11.

  11. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert QbyteArray to int return 0 (problem)

    In example look at this thread

  12. The following user says thank you to Lesiok for this useful post:

    AUDI_ENG (8th July 2014)

  13. #9
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert QbyteArray to int return 0 (problem)

    Quote Originally Posted by Lesiok View Post
    In example look at this thread
    Thank you So MUCH Lesiok

    it's working :

    Qt Code:
    1. int dec = static_cast<quint8>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe
    To copy to clipboard, switch view to plain text mode 

  14. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert QbyteArray to int return 0 (problem)

    You should write like this :
    Qt Code:
    1. int dec = static_cast<int>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe
    To copy to clipboard, switch view to plain text mode 
    Why do two conversions ?

  15. #11
    Join Date
    Jul 2014
    Posts
    26
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert QbyteArray to int return 0 (problem)

    Quote Originally Posted by Lesiok View Post
    You should write like this :
    Qt Code:
    1. int dec = static_cast<int>(oneFrame.at(2)); //example : 2 is the 3th byte in oneframe
    To copy to clipboard, switch view to plain text mode 
    Why do two conversions ?
    I don't know, can you explain me ? (where are the two convertions) ?

Similar Threads

  1. how to convert an int to QByteArray
    By babu198649 in forum Qt Programming
    Replies: 12
    Last Post: 19th September 2014, 09:47
  2. QHttp readAll() return empty QbyteArray
    By chamoda in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2013, 06:59
  3. Replies: 6
    Last Post: 23rd June 2010, 14:48
  4. how can we convert QByteArray to Qstring?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2009, 14:05
  5. Convert from QByteArray to String ?
    By probine in forum Newbie
    Replies: 3
    Last Post: 25th March 2006, 15:49

Tags for this Thread

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.