std::cout gives different output from qDebug

1.1k views Asked by At

I am using Qt, and I have an unsigned char *bytePointer and want to print out a number-value of the current byte. Below is my code, which is meant to give the int-value and the hex-value of the continuous bytes that I receive from a machine attached to the computer:

int byteHex=0;
byteHex = (int)*bytePointer;

qDebug << "\n  int: " //this is the main issue here. 
          << *bytePointer;

std::cout << " (hex:  "
          << std::hex
          << byteHex
          << ")\n";

}

This gives perfect results, and I get actual numbers, however this code is going into an API and I don't want to use Qt-only functions, such as qDebug. So when I try this:

int byteHex=0;
byteHex = (int)*bytePointer;

std::cout << "\n  int: " //I changed qDebug to std::cout
          << *bytePointer;

std::cout << " (hex:  "
          << std::hex
          << byteHex
          << ")\n";

}

The output does give the hex-values perfectly, however the int-values return symbols (like ☺, └, §, to list a few).

My question is: How do I get std::cout to give the same output as qDebug?

EDIT: for some reason the symbols only occur with a certain Qt setting. I have no idea why it happened but it's fixed now.

2

There are 2 answers

0
László Papp On BEST ANSWER

As others pointed out in comment, you change the outputting to hex, but you do not actually set it back here:

std::cout << " (hex:  "
          << std::hex
          << byteHex
          << ")\n";

You will need to apply this afterwards:

std::cout << std::dec;
5
Mike Seymour On

Standard output streams will output any character type as a character, not a numeric value. To output the numeric value, convert to a non-character integer type:

std::cout << int(*bytePointer);