【问题标题】:How to convert a hex value which is in a string to an int?如何将字符串中的十六进制值转换为int?
【发布时间】:2022-11-10 23:44:20
【问题描述】:
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 值(数据)作为字符串存储在 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

每当十六进制值中包含字母时,从 qString 到 uint8_t 的转换都会归零。

【问题讨论】:

    标签: c++ qt uint8t qbytearray


    【解决方案1】:

    正如the documentation of QString::toUint 建议的那样,函数的签名看起来像这样。

    uint QString::toUInt(bool *ok = nullptr, int base = 10) const
    

    第二个参数base 用于指定基数。要从十六进制字符串转换,请使用 16

    speed = samp_buff[3].toUInt(nullptr, 16);
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 2018-01-22
      • 2018-03-20
      • 2013-02-07
      • 2020-12-09
      • 2019-07-27
      • 2020-07-18
      相关资源
      最近更新 更多