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. How to convert a Qstring to QByteArray Such that to read hex values from a file
Forum Updated to NodeBB v4.3 + New Features

How to convert a Qstring to QByteArray Such that to read hex values from a file

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 1.8k Views
  • 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.
  • IndicoderI Offline
    IndicoderI Offline
    Indicoder
    wrote on last edited by Indicoder
    #1

    i need to read a hex value from a file and use it my file has hex values and integer 0 0x05 1 0xf5 2 0x00 3 0x00 4 0x02 and so on...i need to read it from the file and use it to convert those hex value.
    my code is something like this

    void MyUDP::HelloUDP()
    {
    int i;
    QFile file("2.txt");
    if(!file.exists()) qDebug() << "not found file";
    file.open(QIODevice::ReadWrite);
    QByteArray Data;
    // read whole content
    QByteArray content = file.readAll();
    // extract words
    QByteArray a=content.split(" ");
    for(i=0;i<list.size();i++)
    {
    Data.append(list[i]);
    }
    // Data.append(0x05);
    //Data.append(0xf5);need something equivalent to this but read many values from file
    file.close();
    // Sends the datagram datagram
    // to the host address and at port.
    // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
    // const QHostAddress & host, quint16 port)
    socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
    }

    void MyUDP::readyRead()
    {
    //unsigned int inp0,inp1,inp2,inp3;
    //int pos0,pos1,pos2,pos3;
    int temp;
    float roll;
    QByteArray buffer;
    //unsigned int a;
    buffer.resize(socket->pendingDatagramSize());
    QHostAddress sender;
    quint16 senderPort;
    socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
    //a=static_cast<unsigned char>(buffer[1]);
    //scanf(buffer.constData(),"%d\t%x\t%d\t%x\t%d\t%x\t%d\t%x",&pos0,&inp0,&pos1,&inp1,&pos2,&inp2,&pos3,&inp3);
    temp=(static_cast<unsigned char>(buffer[0]) << 8) | static_cast<unsigned char>(buffer[1]);
    roll=temp*0.01;
    //printf("%f",roll);
    qDebug() << "Message from: " << sender.toString();
    qDebug() << "Message port: " << senderPort;
    qDebug() << "Message: " << roll; // it should print 15.25 if my file values are 0x05 0xf5 using buffer[0] and buffer[1] respectively

    }

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

      So what do you want to achieve? Send the file content via udp?

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

      1 Reply Last reply
      0
      • IndicoderI Offline
        IndicoderI Offline
        Indicoder
        wrote on last edited by
        #3

        yes send it through udp and and the file content should not be in string it should take it as a hex value

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

          So just read the content with QFile::readAll() and send it with QUdpSocket::writeDatagram.
          There is no need to convert the data to hex string representation.

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

          1 Reply Last reply
          3
          • IndicoderI Offline
            IndicoderI Offline
            Indicoder
            wrote on last edited by
            #5

            when i send it through readAll() if file contains 0x05 it just prints 0 for buffer[0] and buffer[1] prints x and so on....but i need 0x05 as a whole

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

              How do you print it out? 0x05 is a non-printable character. If you want to print the values out for e.g. debuggin you can for example use QByteArray::toHex() or similar

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

              1 Reply Last reply
              2
              • IndicoderI Offline
                IndicoderI Offline
                Indicoder
                wrote on last edited by
                #7

                void MyUDP::HelloUDP()
                {
                int i;
                QFile file("2.txt");
                if(!file.exists()) qDebug() << "not found file";
                file.open(QIODevice::ReadWrite);

                 QByteArray Data = file.readLine();
                
                 //QByteArray Data;
                 //Data.append(0x05);
                 //Data.append(0xF5);
                
                 file.close();
                
                 socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
                

                }

                void MyUDP::readyRead()
                {
                //unsigned int inp0,inp1,inp2,inp3;
                //int pos0,pos1,pos2,pos3;
                int temp;
                float roll;
                QByteArray buffer;
                //unsigned int a;
                buffer.resize(socket->pendingDatagramSize());
                QHostAddress sender;
                quint16 senderPort;
                socket->readDatagram(buffer.data(), buffer.size(),&sender, &senderPort);
                temp=(static_cast<unsigned char>(buffer[0]) << 8) | static_cast<unsigned char>(buffer[1]);
                roll=temp*0.01;
                //printf("%f",roll);
                qDebug() << "Message from: " << sender.toString();
                qDebug() << "Message port: " << senderPort;
                qDebug() << "Message: " << roll;

                }
                file contains: 0x05 0xF5

                1. if i run the above code as u said reading it from the file its roll output is coming as 124.5 as buffer[0] is 0 and buffer [1] is x

                2. but when i use manual sending the data by Data.append(0x05) ; Data.append(0xF5) then buffer[0] is 0x05 and buffer[1] is 0xF5 and roll output is 15.25 which is correct

                basically i want the second point output using the first method through file

                can you help me out in more detail please

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

                  QFile::readLine() reads a line (which ends with \0) - see documentation. You want QFile::read(int) or QFile::readAll().

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

                  1 Reply Last reply
                  4
                  • IndicoderI Offline
                    IndicoderI Offline
                    Indicoder
                    wrote on last edited by
                    #9

                    sorry i tried with readAll() it was the same

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • IndicoderI Indicoder

                      sorry i tried with readAll() it was the same

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Indicoder said in How to convert a Qstring to QByteArray Such that to read hex values from a file:

                      sorry i tried with readAll() it was the same

                      So you should output via qDebug() and toHex() to see what you've actually read from the file

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

                      1 Reply Last reply
                      1

                      • Login

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