gpt4 book ai didi

c++ - 在消息中显示每个字符的十六进制?

转载 作者:行者123 更新时间:2023-11-28 07:28:11 25 4
gpt4 key购买 nike

想在消息中显示每个字符的十六进制(如果可能的话,简单的方法,我知道我可以遍历字符并执行此操作):

typedef struct{
double dTime;
char cMessage[11];
} typeCanMessage;

typeCanMessage cmMessage1 = {4.5, {0xd2, 0xf1, 0xe0, 0xf1, 0xf1, 0xf1, 0xf1, 0x23, 0x23, 0x23, 0x23}};

QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(QString::number(cmMessage1.dTime, 'f', 8) + " "
+ QString::number(cmMessage1.cMessage, 'x')); // <-- something like this perhaps
msgBox->exec();

错误:“调用重载的‘number(char[11], char)’不明确”

修改:

sprintf(cM, "%x", &cmMessage1.cMessage);

QMessageBox *msgBox = new QMessageBox(0);
msgBox->setGeometry(QRect(QPoint(200,200),QSize(400,400)));
msgBox->setInformativeText(QString::number(cmMessage1.dTime, 'f', 8) + " "
+ (QString)cM); // <-- something like this perhaps
msgBox->exec();

结果:bfb123d8//错误


我想避免执行以下操作(循环或不循环):

 sprintf(cM, "%X%X%X%X%X%X%X%X%X%X%X", cmMessage1.cMessage[0],
cmMessage1.cMessage[1],
cmMessage1.cMessage[2],
cmMessage1.cMessage[3],
cmMessage1.cMessage[4],
cmMessage1.cMessage[5],
cmMessage1.cMessage[6],
cmMessage1.cMessage[7],
cmMessage1.cMessage[8],
cmMessage1.cMessage[9],
cmMessage1.cMessage[10]);

循环:

for(int i = 0; i < 11; i++){
sprintf(cM + 2 * i, "%X", cmMessage1.cMessage[i]);

}

最佳答案

number 函数只是格式化一个数字。如果你想格式化数组中的每个数字,你需要一个循环,按照

auto text = QString::number(cmMessage1.dTime, 'f', 8) + "  ";
for (unsigned c : cmMessage1.cMessage) {
text += QString::number(c,16);
}
msgBox->setInformativeText(text);

(您还应该使用 unsigned char,而不是 char,来存储 8 位无符号值)。

关于c++ - 在消息中显示每个字符的十六进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18319332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com