c++ - Display hex per character in the message? -
would display hex per char in message(simple way if possibe, know can loop through chars , it):
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')); // <-- perhaps msgbox->exec();
error: "call of overloaded 'number(char[11], char)' ambiguous"
modification:
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); // <-- perhaps msgbox->exec();
result: bfb123d8 //wrong
i want avoid doing following(loop or no loop):
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]);
looping:
for(int = 0; < 11; i++){ sprintf(cm + 2 * i, "%x", cmmessage1.cmessage[i]); }
the number
functions format single number. if want format each number in array, you'll need loop, along lines of
auto text = qstring::number(cmmessage1.dtime, 'f', 8) + " "; (unsigned c : cmmessage1.cmessage) { text += qstring::number(c,16); } msgbox->setinformativetext(text);
(you should using unsigned char
, not char
, store 8-bit unsigned values).
Comments
Post a Comment