Results 1 to 4 of 4

Thread: conversion from bytearray to int produce zero

  1. #1
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default conversion from bytearray to int produce zero

    hi,
    can someone help me coz my conversion from socket stream doesnt work (or maybe i'm just blind).. thanks in advance

    Qt Code:
    1. qDebug() << "server sending auth question!!!";
    2. qDebug() << sockdata.toHex();
    3. qDebug() << sockdata.mid(2, 1).toHex() << ":" << sockdata.mid(3, 1).toHex() << ":" << sockdata.mid(4.1).toHex();
    4. bool ok;
    5. quint8 res;
    6. int num1=sockdata.mid(2, 1).toInt(&ok, 10);
    7. int num2=sockdata.mid(3, 1).toInt(&ok, 10);
    8. int oper=sockdata.mid(4, 1).toInt(&ok, 10);
    9. qDebug() << "num1 " << num1 << " num2 " << num2 << " op " << oper;
    To copy to clipboard, switch view to plain text mode 

    and i get zeroes

    output Code:
    1. server sending auth question!!!
    2. "ffdecb7002"
    3. "cb" : "70" : "02"
    4. num1 0 num2 0 op 0
    5. result: 0 "ffdf00000000"
    6. "ffdf00000000"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: conversion from bytearray to int produce zero

    and btw. on Hex base it doesnt work either, so it's really something wrong with my conversion toInt...

  3. #3
    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: conversion from bytearray to int produce zero

    What's about
    Qt Code:
    1. bool ok;
    2. QByteArray ba = QByteArray::fromHex("ffdecb7002");
    3. qWarning() << ba.toHex();
    4. qWarning() << ba.mid(2,1).toHex() << ba.mid(2,1).toHex().toInt(&ok, 16);
    To copy to clipboard, switch view to plain text mode 
    gives
    "ffdecb7002"
    "cb" 203
    ..or say what you expect.

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

    daemonna (12th August 2010)

  5. #4
    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: conversion from bytearray to int produce zero

    I assume that you are expecting to see:
    Qt Code:
    1. "cb" : "70" : "02"
    2. num1 203 num2 112 op 2
    To copy to clipboard, switch view to plain text mode 
    in which case this is where you come unstuck:
    Qt Code:
    1. int num1=sockdata.mid(2, 1).toInt(&ok, 10);
    2. int num2=sockdata.mid(3, 1).toInt(&ok, 10);
    3. int oper=sockdata.mid(4, 1).toInt(&ok, 10);
    To copy to clipboard, switch view to plain text mode 
    You are telling Qt that the QByteArray containing the single byte '0xcb' is an ASCII string of a number in base 10. When it tries to convert that to an int if fails: 0xcb is not an ASCII digit 0-9. The result is 0 and ok will be false (you don't check).
    Try:
    Qt Code:
    1. int num1 = (unsigned char) sockdata.at(2);
    2. int num2 = (unsigned char) sockdata.at(3);
    3. int oper = (unsigned char) sockdata.at(4);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 10
    Last Post: 25th April 2010, 23:59
  2. Replies: 1
    Last Post: 1st October 2009, 21:28
  3. Read ByteArray
    By kewlcode in forum Qt Programming
    Replies: 19
    Last Post: 5th April 2009, 11:41
  4. How to dynamiclly produce the tab by QT/E?
    By xxthink in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2008, 23:08
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13

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.