我正在使用QStringList从配置文件中读取行数/行数。我想将每行的4个字符串转换为4个十六进制值,将此值连接为一个十六进制值,然后通过UART将其发送到(STM32)uC。
示例: 从配置读取: 1200,1200,1200,1200 ->以逗号分割:1200 1200 1200 1200->转换为十六进制:04B0 04B0 04B0 04B0->连接十六进制值: 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()); ?
}
}
那是简单的方法吗?非常感谢你!
答案 0 :(得分:0)
要打印结果,可以结合使用std::hex和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;
这将打印:
4b04b04b04b0
您可以尝试将结果转换回整数。