Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant QBytearray OPCUA problem in reading QString
Forum Updated to NodeBB v4.3 + New Features

QVariant QBytearray OPCUA problem in reading QString

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 801 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by gfxx
    #1

    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

    bkt

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Where exactly are you expecting that 0x ?

      Technically, you will have the correct value in your string 0x is a hint that you will have to add manually if you want to show it along the hex value.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      gfxxG 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        gfxxG 2 Replies Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          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?

          gfxxG Offline
          gfxxG Offline
          gfxx
          wrote on last edited by
          #4

          @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

          bkt

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            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?

            gfxxG Offline
            gfxxG Offline
            gfxx
            wrote on last edited by
            #5

            @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

            bkt

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Where exactly are you expecting that 0x ?

              Technically, you will have the correct value in your string 0x is a hint that you will have to add manually if you want to show it along the hex value.

              gfxxG Offline
              gfxxG Offline
              gfxx
              wrote on last edited by gfxx
              #6

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

              bkt

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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()

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                gfxxG 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  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()

                  gfxxG Offline
                  gfxxG Offline
                  gfxx
                  wrote on last edited by gfxx
                  #8

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

                  bkt

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved