I'm using QStringList to read hunderts/thousends of lines from config-file. I want convert 4 Strings from every row to 4 hex-values, concatenate this values to one hex-value and send it via UART to (STM32) uC.

Example: Read from config: 1200,1200,1200,1200 -> split at comma: 1200 1200 1200 1200 -> convert to Hex: 04B0 04B0 04B0 04B0 -> concatenate hex values: 04B004B004B004B0

if(lines.at(i).contains(",")){
      while(lines.at(i+j) != "\n"){
      QStringList speed_chunks = lines.at(i+j).split(",");
      uart = speed_chunks.at(3)+speed_chunks.at(4)+speed_chunks.at(5)+speed_chunks.at(6)+"\0";

    m1 = speed_chunks.at(3).toInt();
    m1h = QString::number(m1, 16).toUpper();

    m2 = speed_chunks.at(4).toInt();
    m2h = QString::number(m2, 16).toUpper();

    m3 = speed_chunks.at(5).toInt();
    m3h = QString::number(m3, 16).toUpper();

    m4 = speed_chunks.at(6).toInt();
    m4h = QString::number(m4, 16).toUpper();

    uart_hex = m1h+m2h+m3h+m4h +"\0"; WRONG!!!
    //m1hm2hm3hm4h needed, not plus function!

qDebug()<<uart_hex;

m_serial.write(uart()); ?

                               }
                                 }

That is the simple way to do it? Thank you very much!

1 Answers

0
mohabouje On

To print the result, you can use a combination of std::hex and an std::stringstream:

std::stringstream stream;
QStringList items = lines.at(i+j).split(",");
for (auto str : items) {
    const auto trimmed = str.trimmed();
    const auto number = trimmed.toInt();
    stream << std::hex << number;
}

const auto result = stream.str();
std::cout << result;

This will print:

4b04b04b04b0                                                                                                                                                                                                                                                 

You can try to convert back the result into an integer.