十进制类型转换为Hexa [英] type conversion in decimal to Hexa

查看:107
本文介绍了十进制类型转换为Hexa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型转换问题我认为它会非常小但不幸的是我不知道。这是我的代码

type conversion issue i think it would be very small but unfortunately i dont know. here is my code

bool checkok;
    int IDstrdec = frameID.toInt(&checkok, 16);
    int FrameTPDO = IDstrdec & 0xF80;
    int NodeID = IDstrdec & 0x07F;
    qDebug() <<frameID << FrameTPDO <<NodeID;



我的frameID在Qstring中我将它转换为int,以便我可以传递它进行屏蔽。

之后&它以十进制返回结果。但我想要他们在Hexa。我如何在Hexa中得到结果?


my frameID is in Qstring i am converting it into int so that i can pass it for masking.
after & it returns me result in decimal. But i want them in Hexa. how i can get result in Hexa??

推荐答案

您可以将十六进制值存储到字符串中。未经测试

You can store the hex value to string. not tested
#include <stringstream>
bool checkok;
    int IDstrdec = frameID.toInt(&checkok, 16);
std::stringstream FrameTPDO;
std::stringstream NodeID;
    FrameTPDO = << std::hex << IDstrdec;
    NodeID = << std::hex << IDstrdec;
    qDebug() <<frameID << FrameTPDO <<NodeID;


没有'十进制整数和十六进制整数',只有整数。

'十六进制'和'十进制'只是这些数字的不同表示。

您的编译器能够解释以不同表示形式给出的文字,例如' 10 '或' 0xA ',但一旦解析,整数就会被存储,无论其原始表示如何。
There are NOT 'decimal integers and hexadecimal integers', there are just integers.
The 'hexadecimal' and 'decimal' are just different representation of such numbers.
Your compiler is able to interpret literals given in different representations, like '10' or '0xA', but once parsed, integers are stored, regardless of their original representation.


正如其他人已经注意到的那样。这只是格式化输出的问题。使用Qt,你可以创建一个字符串来传递给 qDebug()

As already noted by others it is only a problem of formatting the output. With Qt you may create a string to be passed to qDebug():
QString debugMsg = QString("%1 %2 %3")
    .arg(frameID, 0, 16)
    .arg(FrameTPDO, 0, 16)
    .arg(NodeID, 0, 16);
qDebug(debugMsg);



参见 QString :: arg(int ...) [ ^ ]用于使用的格式选项。


See QString::arg(int ...)[^] for the used format options.


这篇关于十进制类型转换为Hexa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆