Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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

    }


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.



  • 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


  • Lifetime Qt Champion

    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



  • 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


  • Lifetime Qt Champion

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



  • sorry i tried with readAll() it was the same


  • Lifetime Qt Champion

    @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


Log in to reply