字符串转10进制,10进制转字符串,并移16进制数显示

 
        ID=dateLine->text();//得到字符串
        QString str = ID;
        bool ok;
        int hex = str.toInt(&ok, 16);       // 将字符串变成16机制数
        hex=hex+1;
 
        QString t = QString::number(hex, 16).toUpper();将数字变成16机制数,并以大写显示
        dateLine->setText(t);显示在textveiw() 里面
 
        cout<< hex<<endl;
    
 
16进制转换。
这是一段非常经典的代码,主要用在字符串与 int 类型的转换。
实现功能:
1、将字符串按照16进制数,自增,只用修改
   int hex = str.toInt(&ok, 10);
  hex=hex+1;

2、将字符串按照10进制数,自增
   int hex = str.toInt(&ok, 16);
  hex=hex+1;
3、将字符串转成16进制数,然后以10进制数显示。
 int hex = str.toInt(&ok, 16);   
  QString t = QString::number(hex, 10).toUpper();
4、将字符串转成10进制数,然后以16进制数显示
int hex = str.toInt(&ok, 10);   
  QString t = QString::number(hex, 16).toUpper();

原理:
int QString::toInt(bool * ok = 0, int base = 10) const
Returns the string converted(转换) to an int using base base, which is 10 by default and must be between 2 and 36(转换进制数,只能是2到36,二进制,10进制 ,9进制等都能够转换了), or 0. Returns 0 if the conversion fails.

If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
转换成功,返回值OK为1,返回 错误,则值为1.所以 还可以对OK值进行 利用判断,转换是否成功。

If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.

The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()



yearLine->setText((QString::number(CMD.trimmed().toInt(), 10)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值