I want to take 20 byte hex input from QlinEdit
and I want to validate QlinEdit
for only 20 bytes and also store it into QString
.
I have Done for trial purpose :
ui->SetValue->setMaxLength(4);
ui->SetValue->setInputMask("Hh hh hh hh");
But It is giving me 11 bytes instead of 4 bytes.
and the hex values are not coming in pair in QString
. What Should I do?
I have tried everything but I'm not able to do that.
I have used:
ui->SetValue->setInputMask("Hh hh hh hh");
else if (ui->checkBox_HEX->isChecked())
{
ui->SetValue->setEnabled(true);
obj = ui->SetValueUniqueId->text();
QByteArray bytes = obj.toLatin1();
int length = myHexArray.size(); //Number of bytes
printf("Number Of bytes = %d", length);
memcpy(buffer, obj.toStdString().c_str(), obj.size());
As I never used
QLineEdit::setInputMask()
before, I made an MCVE – to provide an answer as well as for my own entertainment.There are two essential parts in this sample:
sets the input mask of
QLineEdit
to accept 20 × 2 characters for which "Hexadecimal character required. A-F, a-f, 0-9." (Remember, one byte → two hex digits.)(Note that
h
in opposition permits hex digits but does not require.)The other part is in the lambda which I used as signal handler to convert the hex digit input into the corresponding byte values:
Thereby,
text
is aQString
with the current text of theQLineEdit qTxtIn
(the signal sender). (Alternatively, I could have usedqTxtIn.text()
.)QString
uses internally some kind of Unicode encoding (on Windows probably UTF-16).The
QByteArray::fromHex()
can interprete text input decoding hex-digits to byte values. The only issue – it expects aQByteArray
as input.Therefore, the
QString
is converted toQByteArray
(before applied toQByteArray::fromHex()
) using the methodQString::toLatin1()
. Consider, that Latin1 provides much less characters than Unicode. However, in the case of hex-digits this really is no problem as the digits 0 ... 9 as well as the letters a ... f (and A ... F) are available in Latin1 also.The rest is what I considered entertainment – in my case converting the bytes to a C string to make them printable again.
The complete sample code
testQLineEdit-Hex.cc
:I compiled and tested in VS2013 with Qt 5.9.2 on Windows 10 (64 bit):
As you can see, I praticed my memory about ASCII values...