PDA

View Full Version : Hex to Ascii and Ascii to Hex Problem



havoc
19th July 2013, 19:07
hi all,
This is my code:

#include <QCoreApplication>
#include <QDebug>
QString HexToAscii(QString Str);
QString AsciiToHex(QString Str);

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

qDebug() << HexToAscii("4170706c65");
qDebug() << AsciiToHex(HexToAscii("4170706c65").toLocal8Bit());

qDebug() << HexToAscii("410070706c65"); // "00" is added after "41"
qDebug() << AsciiToHex(HexToAscii("410070706c65").toLocal8Bit());

return a.exec();
}


QString HexToAscii(QString String)

{

QByteArray ByteArray1=String.toUtf8();

const char* chArr1=ByteArray1.constData();

QByteArray ByteArray2=QByteArray::fromHex(chArr1);

const char* chArr2=ByteArray2.constData();

return QString::fromUtf8(chArr2);

}


QString AsciiToHex(QString String)

{

QByteArray ByteArray1=String.toUtf8();

QByteArray ByteArray2=ByteArray1.toHex();

const char* chArr1=ByteArray2.constData();

return QString::fromUtf8(chArr1);

}

And the Output is:
"Apple"
"4170706c65"
"A"
"41"


why the "00" is causing the problem there? I want the last line to be "410070706c65"
Sorry but I'm very new to Qt, please help me with the solution and the reason behind the unexpected value.
thanks in advance.

Santosh Reddy
19th July 2013, 21:13
Please specify the platform/Qt Veriosn/ABI (32/64/... bit) information.

ChrisW67
19th July 2013, 23:05
why the "00" is causing the problem there? I want the last line to be "410070706c65"
Sorry but I'm very new to Qt, please help me with the solution and the reason behind the unexpected value.
thanks in advance.
C-strings are terminated by NUL (i.e. '\0') character (nothing to do with Qt). Feeding an array of bytes contain zeroes to any function expecting a C-string (i.e. const char *) will behave this way: this includes the Qt function QString::fromUtf8().

QByteArray is the Qt data type that handles arrays of arbitrary bytes. Your HexToAscii() function is what QByteArray::fromHex() does. Your AsciiToHex() is precisely what QByteArray::toHex() does. All the other stuff you do in those functions is unnecessary and ultimately breaks your expectations.

havoc
20th July 2013, 06:06
thanks you for your responses. ChrisW67, can you show me an example on how I can deal with the NUL? I am basically wanting it for a program which writes(using socket->write() ) the data containing NUL somewhere in the middle of the string, often many NULs inbetween. How can I manage it?

ChrisW67
20th July 2013, 06:34
You use the version of QIODevice::write() that takes a QByteArray or a const char * with a size, and not the version that expects a nul terminated c string. None of this has anything to do with converting bytes to or from hexadecimal.

havoc
20th July 2013, 09:30
Thank you, I'll look onto that. Actually the packet data I am trying to send is like the ASCII form of "02000204690001000001" and its hard for me to directly send the packet in ASCII form so I was trying to store the packet in hexadecimal form in a variable and use it at the time of sending by converting it to ASCII using the HexToAscii converter and later after receiving the bytes in ASCII form I want to display them in Hex form using the AsciiToHex converter. I have done this easily in Visual Basic 6 and I recently started learning Qt so I'm facing too many problems. I searched many times still found nothing. I don't know if I'm asking for too much, but can you give me a clue or any link where I can learn this from?

ChrisW67
20th July 2013, 23:24
Actually the packet data I am trying to send is like the ASCII form of "02000204690001000001"
What do you mean "the ASCII form of" in relation to a perfectly good ASCII string?

I think you more urgently need to understand what hexadecimal is: it is a human-readable representation of a number in base 16. A hexadecimal string is an ASCII string representing a collection of bytes that may not have values valid in an ASCII string, i.e. any byte value over 127, or inconvenient in a string like zero bytes.


Assuming that string is in a QString variable s:


QByteArray buf = s.toLatin1();
iodev.write( buf );

Will send that string through the device. That is a series of 20 bytes with values (decimal): 48 50 48 48 48 50 etc.

If you mean the string is the hexadecimal representation of a series of 10 bytes then:


QByteArray buf = QByteArray::fromHex( s.toLatin1() );
iodev.write( buf );

Will write ten bytes with the values (decimal): 2 0 2 4 105 0 1 0 0 1

There is no need to convert binary data to hexadecimal strings in Qt unless you are doing it to display to a human.