How to convert ASCII Hex string to Byte array?
kahlenberg last edited by kahlenberg
I try to convert a string, containing hex chars only, to a byte array. I could do it in C but in Qt I did not find any solution :) . Yes it is easy and trivial but really I need help.
Lets say I have a string like "E00200BA9EFC00AC", how can I convert it to an array like this:
Do you mean something like QByteArray::formHex ?
@SGaist Thanks for answer, almost that. But it takes a QByteArray as parameter. I have a string. How can I convert a string to a QByteArray ? Every two character in my string represents a byte.
Do you mean a std::string or "MyHexString" ?
@SGaist QString MyHexString = "E00200BA9EFC00AC";
Then why not do as the example in the doc suggests:
QByteArray myHexArray = QByteArray::fromHex("E00200BA9EFC00AC");
Becausue fromHex accepts QByteArray as parameter, I have QString. Later I will get this string from UI element, QLineEdit
(ui->cmdToWrite->text(), this is QString)
QString MyHexString = "E00200BA9EFC00AC"; QByteArray cmd = QByteArray::fromHex(MyHexString);
D:\Qt\Projects\Widget\mainwindow.cpp:40: error: no matching function for call to 'QByteArray::fromHex(QString&)'
QByteArray cmd = QByteArray::fromHex(MyHexString);
jduran_gm last edited by
@kahlenberg Take a look to this member function:
QByteArray QByteArray::fromHex(const QByteArray & hexEncoded)
Again, why create a QString ? Just use QByteArray directly or if you really really want a QString, use e.g. toLatin1
@SGaist Oh, thank you very much, toLatin1() solved my problem. I didn't see it :) Thanks again.