Question

I have a QString where I append data input from the user.

At the end of the QString, I need to append the hexadecimal representation of a "Normal" QString.

For example:

QString Test("ff00112233440a0a");
QString Input("Words");

Test.append(Input);//but here is where Input needs to be the Hex representation of "Words"

//The resulting variable should be
//Test == "ff00112233440a0a576f726473";

How can I convert from ASCII (I think) to it's Hex representation?

Thanks for your time.

Was it helpful?

Solution

You were very close:

Test.append(QString::fromLatin1(Input.toLatin1().toHex()));

OTHER TIPS

Another solution to your problem.

Given a character, you can use the following simple function to compute its hex representation.

// Call this function twice -- once with the first 4 bits and once for the last
// 4 bits of a char to get the hex representation of a char.
char toHex(char c) {
   // Assume that the input is going to be 0-F.
   if ( c <= 9 ) {
      return c + '0';
   } else {
      return c + 'A' - 10;
   }
}

You can use it as:

char c;
// ... Assign a value to c

// Get the hex characters for c
char h1 =  toHex(c >> 4);
char h2 = toHex(c & 0xF);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top