QString samp_buff[100];
QByteArray data;
uint8_t speed;
samp_buff[3] = data.toHex(); //I converted the QByteArray into a string
qDebug() << "read_every_data_"<< samp_buff[3];
speed = samp_buff[3].toUInt(); //Trying to convert the string to uint8_t
qDebug() << "Converted to UINT8" << speed;
嗨!我成功地将Qbytearray
值(数据)作为QString
存储在字符串的samp_buff
数组中,并且在以十六进制形式将QString
转换为uint8_t
时也是如此。
Data: "\x07" //QByteArray
read_every_data_ "07" //QString
Converted to UINT8 7 //Uint8_t
它可以正常工作,但当这种情况发生时,问题就出现了。
Data: "\x0B" //QByteArray
read_every_data_ "0b" //QString
Converted to UINT8 0 //Uint8_t
每当十六进制字符串中包含字母表时,转换结果为零。
发布于 2022-10-26 21:27:32
正如the documentation of QString::toUint
所建议的,函数的签名如下所示。
uint QString::toUInt(bool *ok = nullptr, int base = 10) const
第二个参数base
用于指定基。若要从十六进制字符串转换,请向其提供16
。
speed = samp_buff[3].toUInt(nullptr, 16);
https://stackoverflow.com/questions/74217149
复制