Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert an int to QByteArray in Qt

Tags:

c++

qt

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;
like image 422
andreahmed Avatar asked Apr 21 '16 06:04

andreahmed


People also ask

How do you convert int to QString?

Here's for an int: int i = 42; QString s = QLocale(). toString(i);

What is QByteArray in Qt?

QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char * .

How do I convert an int to a byte?

An int value can be converted into bytes by using the method int. to_bytes(). The method is invoked on an int value, is not supported by Python 2 (requires minimum Python3) for execution.


2 Answers

you can simply do like this.

int myInt = 123;
QByteArray q_b;
q_b.setNum(myInt);
like image 182
Kamlesh Avatar answered Sep 21 '22 18:09

Kamlesh


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

QByteArray::number

like image 22
Anonymous Avatar answered Sep 23 '22 18:09

Anonymous