C macro in C ++ \ Qt

I have the following c macro from the yahoo libpurple plugin:

#define yahoo_put16(buf, data) ( \
        (*(buf) = (unsigned char)((data)>>8)&0xff), \
        (*((buf)+1) = (unsigned char)(data)&0xff),  \
        2)

I want to implement the same thing as a function in my class that takes quint16 as a parameter and returns it as QByteArray I have the following, but I don't seem to get the same result as with the macro above.

QByteArray YahooPacket::packQuint16(quint16 value) const
{
    QByteArray data;

    data.append(QByteArray::number((value >> 8) & 0xFF));
    data.append(QByteArray::number(value & 0xFF));

    return data;
}

How do I do to implement my function?

+2
source share
1 answer

QByteArray::number()creates a print (string) version of the number, which is probably not what you want. Use a constructor QByteArraythat takes a pointer to a buffer and a size parameter. I think this will do what you want.

QByteArray YahooPacket::packQuint16(quint16 value) const
{
    QByteArray data;

    data.append(QByteArray(((char*)&value)+1,1));
    data.append(QByteArray((char*)&value,1));

    return data;
}
+2
source

Source: https://habr.com/ru/post/1768994/


All Articles