QVariant QBytearray OPCUA problem in reading QString
-
I have a QVariant Qbytearray hex to convert in litteral QString .... I try in these way without success ...
QString text = value.toByteArray().toHex(); /// value is Qvariant came from QOpcUa::NodeAttribute::Value ... QString textValue = QByteArray::fromHex(text.toLatin1());value is like these fedfabdc..... there is not 0x separator but value is hex .... and need to convert in litteral like "pump" ...
i appreciate any help.
regards
-
Hi,
Where exactly are you expecting that
0x?Technically, you will have the correct value in your string
0xis a hint that you will have to add manually if you want to show it along the hex value. -
What format has your incoming data? And what do you want to display?
value is like these fedfabdc
Is this a string or the actual byte values?
-
What format has your incoming data? And what do you want to display?
value is like these fedfabdc
Is this a string or the actual byte values?
@Christian-Ehrlicher said in QVariant.QBytearray.toHex convert to litteral text QString:
Is this a string or the actual byte values?
These is value converted in string for debug .... but I receive from opcua server that value so not know exactly every time the content of string .... for sure from server start a string like "good icecream" but i receive only a bytestring i think (not sure at all) .... I use qtopcuaviewer example as debug and starting point of my app .... so that I don't know exactly what datatype a string is converted into by the plugin ... it looks like a bytestring to me .... but the original qtopcuaviewer code just reads the qbytearray.toHex and if I convert to strings directly the qvariant qropcuaviewer emits a warming to the terminal where it says that the data type is not implemented (open64541) ..... so I search for a workaround .... thats all .... or there are a way to know what datatype is that data (containing on qvariant "value") ?
regards
-
What format has your incoming data? And what do you want to display?
value is like these fedfabdc
Is this a string or the actual byte values?
@Christian-Ehrlicher exactly the return string of
if (typeNodeId == QLatin1String("ns=0;i=12")){ qDebug() << "data type is?: " << value; QString text = value.toByteArray().toHex(); return text; }qDebug() << value; return these ..... QVariant(QString, "\u0000\u0000\u0000\u0000\u000E@��\u0005��ᬰ��\u000BP��\u0000p��(\u0000W�\u0003\u0000\u0000�\u0007
��\u0007��")and return text; return correctly (correctly as function need to work ... but for sure non interesting value) these value: 000000000e40efbfbdefbfbd05efbfbdefbfbde1acb0efbfbdefbfbd0b50efbfbdefbfbd0070efbfbdefbfbd280057efbfbd030000efbfbd0760efbfbdefbfbd0760efbfbdefbfbd
regards
-
Hi,
Where exactly are you expecting that
0x?Technically, you will have the correct value in your string
0xis a hint that you will have to add manually if you want to show it along the hex value.@SGaist sorry i find the real problem .... it become from these function:
if (value.canConvert<QString>()){ /* <------ "canConvert" QString fx = "false"; QString tx = "true"; QString wx = value.toString().toUtf8(); if(wx != fx && wx != tx) qDebug() << " value.canConvert<QString>() : " << value.toString().toUtf8(); /* these convert nothing .... so only true and false */ return wx; }canConvert is a Qtopcua function for convert QVariant value into QString .from plugin real data .... on open64541 library these seems not work properly .... and conversion on QString of 50 caracters become wrong.
Value "true" and "false" is correctly converted because start from int value in qvariant chain .... these function return only true and false ..... my debug code show nothing because canConvert can convert only in false or true .... -
So there is no string in your data but only a bunch of bytes - therefore canConvert<QString> and value.toString() is plain wrong. It's a QByteArray so treat it as such.
If you want to show a QByteArray as hexadecimal values you have to use QByteArray::toHex() -
So there is no string in your data but only a bunch of bytes - therefore canConvert<QString> and value.toString() is plain wrong. It's a QByteArray so treat it as such.
If you want to show a QByteArray as hexadecimal values you have to use QByteArray::toHex()@Christian-Ehrlicher said in QVariant.QBytearray.toHex convert to litteral text QString:
So there is no string in your data but only a bunch of bytes - therefore canConvert<QString> and value.toString() is plain wrong. It's a QByteArray so treat it as such.
If you want to show a QByteArray as hexadecimal values you have to use QByteArray::toHex()Sorry at all ... but seems server side string was empty or bunch (server is not mine) ... so first reading data result was bunch data ... but I think was error in reading my app side .... after these I use qtopcuaviewer and see the same ... so I think was some unsolved question on open64541 side ....
for better understand the actual working code was:
if (typeNodeId == QLatin1String("ns=0;i=12")){ if (value.canConvert<QString>()){ QString sx = value.toString().toUtf8(); QString res = sx.mid(0, sx.indexOf("\'")); return res; } }but now after server string was full and not empty like previously ... work these too:
if (typeNodeId == QLatin1String("ns=0;i=12")){ return value.toByteArray(); }Second obviusly is correct ....
I changed post title .... think more easy to find for thus can have these issue.