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

How to convert a qstring to hexadecimal



  • I am developing an interface where it is necessary to add a value in hexadecimal by means of a QLineEdit, I already have that part including the saving of the data, but I do it in String, I want to know how to save it in herxadecimal, preserving the format, for example, if I write in the QLineEdit: A2 I want to save it in a variable in that same format but in hexadecimal. I don't know if I'm clear, if someone understands what I want to do, can they help me please
    Somthing like this.
    This is a QLineEdit
    ![alt text]f6f62ef6-2adc-4234-b0ed-717603d8411c-image.png
    I want sonthig like this![alt text]46ace04c-6d93-4e13-a19a-6a4a0b347307-image.png (image url)


  • Lifetime Qt Champion

    Hi
    Hex is a representation and not a basic true value so you cant really save it a variable as hex.
    but you can convert a string between hex and decimal like this

    QString str = "FF";
    bool ok;
    int hex = str.toInt(&ok, 16);     
    int dec = str.toInt(&ok, 10);    
    


  • @mrjj Hi thanks
    Coul you help me, i want to print this value on SerialPort but need to a on hex, how can i do it??


  • Lifetime Qt Champion

    @Morado
    You can use a Qbytearray and send that
    QByteArray array = QByteArray::fromHex( ui->xtxtAdress->text());
    serial.write(array);



  • @mrjj
    Thanks very much!
    It helped me a lot.


Log in to reply