Conversion from QByteArray to int



  • Hi,

    I have a QByteArray and VS2015 during debugging phase gives:

    [size] 2
    [referenced] 1
    [0] 1 '\x1'
    [1] 0 '\0'

    I would like to convert this QByteArray to int.
    So I write:

    int count = info.toInt(&ok, 16);

    The result is count = 0 instead 1 and the conversion is false

    Could you help please



  • @mulfycrowh
    toInt() expects that the QByteArray contains a string, but in your case, it's a list of bytes (the first bte contains the numerical value 1 and not the character "1").
    It's not clear how your array is structured but you can try something like that:

    int count = int(info[0]);
    for (int i=1; i<info.length(); ++i) {
       int b = int(info[i]);
       if (b > 0) {
          count = (count << 8) + b;
       } else {
          break;
       }
    }
    

    Update: thinking about your terminating \0: Are you sure you expect the value 1 and not 256?
    A null terminated array which does not represent a string does not makes sense so my code is an example but does not make sense, too....



  • As @micland said, QByteArray::toInt() expects an ASCII string.

    Its not clear why you have two bytes, but if:

    a) you only care about the first byte, then its a simple as:

    int count = info.at(0);
    

    b) if, for example, its a 16-bit integer packed into two bytes, then:

    #include <QtEndian>
    ...
    const int count = qFromLittleEndian<qint16>(info.data());
    

    Cheers.



  • I tried something else that runs pretty well but in the wrong order:

    info.toHex().toInt(&ok, 16);
    

    The result is 256.
    It means that it has been read in the wrong order.
    How can I specify that first byte is LSB and second one MSB ?



  • @mulfycrowh said in Conversion from QByteArray to int:

    info.toHex().toInt(&ok, 16);
    

    The intermediate conversion to hex is unnecessary.

    The result is 256. It means that it has been read in the wrong order.

    Whether that code will produce the right result or not, depends on the endianness of the host. In your case, as you said, its the wrong order. On another platform, it would be the correct order.

    How can I specify that first byte is LSB and second one MSB

    Exactly as I suggested above :)

    #include <QtEndian>
    ...
    const int count = qFromLittleEndian<qint16>(info.data());
    

    Here the qFromLittleEndian function knows how to convert from little-endian (least significant byte first), to whatever endianness your host is (which may or may not be the same endianness).

    Give it a go.

    Cheers.



  • I tried what you suggested:

    qFromLittleEndian<quint16>(info.data());
    

    I've got the compiler error :

    C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types



  • I mean qint16.
    Same issue



  • @mulfycrowh said in Conversion from QByteArray to int:

    I've got the compiler error :
    C2665 'qFromLittleEndian': none of the 2 overloads could convert all the argument types

    Ah... from the qFromLittleEndian docs:

    Note: Since Qt 5.7, the type of the src parameter is a void pointer.

    For pre-5.7, you will need to cast to uchar, like:

    int count = qFromLittleEndian<qint16>(reinterpret_cast<const uchar *>(info.data()));
    

    (I just tested that on Qt 5.5.1)

    Cheers.



  • thanks, it runs perfectly !


  • Qt Champions 2016

    @mulfycrowh
    A two byte sequence is a short, not int. You should use the data stream classes for such deserializations instead of going to type-safety hell:

    QByteArray data; //< This is your data
    
    QDataStream in(data); //< Attach a read-only stream to it
    in.setByteOrder(QDataStream::LittleEndian); //< Set the proper byte order
    
    qint16 result; //< The result you want
    in >> result; //< Just read it from the stream
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.