# how to put hex numbers into a Qbytearray?

• Hello,
Can anyone suggest me about how to use hex number which are in ASCII form as a Qbytearray?
Actually I am converting my decimal number into hex number which is then stored as its ascii character using this function

``````QByteArray double2HexString(double n)
{
unsigned char hexaDeciNum;
QByteArray hexbyte ;
int i = 0;
while(n!=0)
{
int temp  = 0;
temp = n % 16;
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
for(int j=i-1; j>=0; j--)
cout << hexaDeciNum[j];

hexbyte = QByteArray((char*)hexDeciNum,20);
return hexbyte;
}
``````

which then I need to send to another function which is storing the hex number into an array using this function byte by byte.

``````void writeout_RS5(){
QByteArray writedata_RS5;
QByteArray value;
qDebug()<<"helloooooooooo";
writedata_RS5=double2HexString(data);
value=  writedata_RS5;
value= writedata_RS5;
qDebug()<<writedata_RS5.toHex();
``````

Where m I wrong?
What can I do now?
Can anyone help?

[Added code tags ~kshegunov]

• @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

• @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

Or even a QByteArray::number since you want to store the result in a QByteArray

• @mostefa if we go that far, QByteArray accepts an int value as constructor:

``````QByteArray byteArray1(61455);
//or
QByteArray byteArray2(0xF00F);
``````

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