7

I'm trying to convert an int to QByteArray. I'm using this method QByteArray::number(m_vman); is it correct?

I'm trying to use the number() for getting an int to QByteArray.

I'm trying the following code but the bytearray is zero

    QByteArray vmanByteArray, vheaterByteArray;
    QDataStream streamVMan(&vmanByteArray, QIODevice::WriteOnly);
    QDataStream streamVHeater(&vheaterByteArray, QIODevice::WriteOnly);

    streamVMan << m_vman;
    streamVHeater << m_vheater;

QByteArray arr = m_htman ? vmanByteArray : vheaterByteArray;
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
andreahmed
  • 135
  • 1
  • 1
  • 9
  • Possible duplicate of [Storing integer to QByteArray using only 4 bytes](http://stackoverflow.com/questions/13668827/storing-integer-to-qbytearray-using-only-4-bytes) – IAmInPLS Apr 21 '16 at 06:39
  • Your code works. This question is off-topic as such. You haven't even bothered to check if your own code works :( – Kuba hasn't forgotten Monica Apr 26 '16 at 16:47

5 Answers5

11

you can simply do like this.

int myInt = 123;
QByteArray q_b;
q_b.setNum(myInt);
Kamlesh
  • 629
  • 7
  • 9
3

Related to the actual question title, the following static function should definitely work:

QByteArray::number

Anonymous
  • 4,617
  • 9
  • 48
  • 61
1

I flagged as a duplicate because you could have searched better (seriously, there are dozen of questions like that). Anyway, this is the easiest solution:

int myInt;
QByteArray bA;
QDataStream stream(&bA, QIODevice::WriteOnly);
stream << myInt;
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
1

This thread is quite old as I just stumbled across the issue (or a similar one), here is my suggestion:

To store the value of the integer variable (what the value is is not exactly defined here, I mean the memory representation in bytes) in a QByteArray I suggest:

QByteArray arr = QByteArray::fromRawData(reinterpret_cast<const char *>(&m_vman), sizeof(m_vman));

Test case:

#include <QtCore>

int main() {
    qint64 intValue = 42;

    QByteArray bytes = QByteArray::fromRawData(reinterpret_cast<const char *>(&intValue), sizeof(intValue));

    qDebug() << "intValue: " << intValue;
    qDebug() << "bytes:    " << bytes.toHex(':');
}

Output (bytes is little endian representation):

intValue:  42
bytes:     "2a:00:00:00:00:00:00:00"
Mike Feustel
  • 1,277
  • 5
  • 14
0

The array is not zero, your code works fine:

#include <QtCore>

QByteArray test(bool m_htman) {
   int m_vman = 1;
   int m_vheater = 2;
   QByteArray vmanByteArray, vheaterByteArray;
   QDataStream streamVMan(&vmanByteArray, QIODevice::WriteOnly);
   QDataStream streamVHeater(&vheaterByteArray, QIODevice::WriteOnly);

   streamVMan << m_vman;
   streamVHeater << m_vheater;

   QByteArray arr=m_htman ?  vmanByteArray: vheaterByteArray;
   return arr;
}

int main() {
   auto one = QByteArray() + '\0' + '\0' + '\0' + '\1';
   auto two = QByteArray() + '\0' + '\0' + '\0' + '\2';
   Q_ASSERT(test(true) == one);
   Q_ASSERT(test(false) == two);
}

It may be "zero" if the values stored in it are zeroes, but that'd be correct.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313