0

Would like to display the hex per char in the message(simple way if possibe, I know I can loop through the chars and do 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')); // <-- something like this perhaps
msgBox->exec();

Error: "call of overloaded 'number(char[11], char)' is 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); // <-- something like this perhaps
msgBox->exec();

Result: bfb123d8 //wrong


I want to avoid doing the 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 i = 0; i < 11; i++){
    sprintf(cM + 2 * i, "%X", cmMessage1.cMessage[i]);

}
jdl
  • 6,151
  • 19
  • 83
  • 132
  • I'm a little confused... what's the difference between "hex per character" and "hex of the combined characters?" Should `"ABC"` be displayed as `0x41 0x42 0x43`? – Kyle G. Aug 19 '13 at 17:17
  • Sidebat: Enable your compiler warnings. Initializing a `char[]` array with values > `0x7F` could be problematic. At least my compiler (Apple LLVM 4.2) certainly complains about it. – WhozCraig Aug 19 '13 at 17:18

3 Answers3

1

The number functions just format a single number. If you want to format each number in the array, you'll need a loop, along the lines of

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

(You should also be using unsigned char, not char, to store 8-bit unsigned values).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

The beauty of hexadecimal is that it translates in a way that can be broken up.

Look at answers here: C++ convert string to hexadecimal and vice versa

FredOverflow uses:

A string like "Hello World" to hex format: 48656C6C6F20576F726C64.

Watch this: "H_e_l_l_o_ _W_o_r_l_d" = 48_65_6C_6C_6F_20_57_6F_72_6C_64.

As anyone who's worked with web development would be able to point out, 20 is space.

You can convert the whole thing to hex and then just insert spaces into the hex string, or split it up by pairs, etc.

Take a peek: http://www.asciitable.com/

Why does this work? It has to do with the nature of binary and hex. because hex, base 16, is a multiple of binary, base 2,

72_dec = H_letter = 01001000_2 = 0100 1000 = 4 8 = 48_hex. You can split it up. So "Hi!" = 486921 in hex, and each individually is 48 69 21 (H i !)

Community
  • 1
  • 1
Plasmarob
  • 1,321
  • 12
  • 20
0

You can use std::hex to do that:

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <string>

int main()
{
    std::string input = "";
    std::getline(std::cin, input);
    std::cout << "Standard Output:  " << input.c_str() << std::endl;
    std::cout << "Hex Output:  " << std::hex;
    std::copy(input.begin(), input.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42