Results 1 to 2 of 2

Thread: QByteArray char to int

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QByteArray char to int

    Hi,

    I have the following code:

    Qt Code:
    1. quint64 input251 = Q_UINT64_C(2161727824133970762);
    2. QByteArray in251 = QByteArray::fromRawData((char *)&input251, sizeof(input251));
    To copy to clipboard, switch view to plain text mode 

    the first 4 bytes are a time value (to be processed later)
    the next 4 are an id value.

    in251 holds the value: "J[²

    This piece of code:
    Qt Code:
    1. bool ok;
    2. msgTime = in251.left(4).toUInt(&ok);
    3. if (!ok) printf ("Conversion ERROR!\r\n" );
    To copy to clipboard, switch view to plain text mode 

    always returns "Conversion error" and msgTime, of course, is set to zero.

    Why can't .toUint() convert those bytes to an int?

    Already thankful for any thoughts,
    Pedro Doria Meunier

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QByteArray char to int

    Quote Originally Posted by pdoria View Post
    in251 holds the value: "J[²

    This piece of code:
    Qt Code:
    1. bool ok;
    2. msgTime = in251.left(4).toUInt(&ok);
    3. if (!ok) printf ("Conversion ERROR!\r\n" );
    To copy to clipboard, switch view to plain text mode 

    always returns "Conversion error" and msgTime, of course, is set to zero.

    Why can't .toUint() convert those bytes to an int?
    ? Have you looked what in251.left(4) gives you back if the value of in251 is "J[²"? That can't work. Use QString::number() to convert a number in a string/byte array!
    Qt Code:
    1. qWarning() << QString::number(input251).left(4).toInt();
    To copy to clipboard, switch view to plain text mode 

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

    pdoria (16th July 2009)

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. QByteArray problem
    By Misenko in forum Qt Programming
    Replies: 17
    Last Post: 4th October 2008, 21:53
  3. char * problem
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:06
  4. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  5. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01

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.