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.
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.