I want to work with unsigned 8-bit variables in C++. Either unsigned char
or uint8_t
do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t
is just an alias for unsigned char
, or so the debugger presents it.
The problem is that if I print out the variables using ostream in C++ it treats it as char. If I have:
unsigned char a = 0;
unsigned char b = 0xff;
cout << "a is " << hex << a <<"; b is " << hex << b << endl;
then the output is:
a is ^@; b is 377
instead of
a is 0; b is ff
I tried using uint8_t
, but as I mentioned before, that's typedef'ed to unsigned char
, so it does the same. How can I print my variables correctly?
Edit: I do this in many places throughout my code. Is there any way I can do this without casting to int
each time I want to print?
I have used in this way.
Use:
And if you want padding with leading zeros then:
As we are using C-style casts, why not go the whole hog with terminal C++ badness and use a macro!
you can then say
Edit: Having said that, MartinStettner's solution is much nicer!
This will also work:
I'd like to post my re-re-inventing version based on @FredOverflow's. I made the following modifications.
fix:
operator<<
should be ofconst
reference type. In @FredOverflow's code,h.x >>= 4
changes outputh
, which is surprisingly not compatible with standard library, and typeT
is requared to be copy-constructable.CHAR_BITS
is a multiple of 4. @FredOverflow's code assumeschar
is 8-bits, which is not always true, in some implementations on DSPs, particularly, it is not uncommon thatchar
is 16-bits, 24-bits, 32-bits, etc.improve:
std::uppercase
. Because format output is used in_print_byte
, standard library manipulators are still available.hex_sep
to print separate bytes (note that in C/C++ a 'byte' is by definition a storage unit with the size ofchar
). Add a template parameterSep
and instantiate_Hex<T, false>
and_Hex<T, true>
inhex
andhex_sep
respectively._print_byte
is extracted out ofoperator<<
, with a function parametersize
, to avoid instantiation for differentSize
.More on binary code bloat:
As mentioned in improvement 3, no matter how extensively
hex
andhex_sep
is used, only two copies of (nearly) duplicated function will exits in binary code:_print_byte<true>
and_print_byte<false>
. And you might realized that this duplication can also be eliminated using exactly the same approach: add a function parametersep
. Yes, but if doing so, a runtimeif(sep)
is needed. I want a common library utility which may be used extensively in the program, thus I compromised on the duplication rather than runtime overhead. I achieved this by using compile-timeif
: C++11std::conditional
, the overhead of function call can hopefully be optimized away byinline
.hex_print.h:
hex_print.tcc:
test:
output:
de ad be ef DEADBEEF
I would suggest using the following technique:
It's short to write, has the same efficiency as the original solution and it lets you choose to use the "original" character output. And it's type-safe (not using "evil" macros :-))
You can try the following code:
Output:
a is 0; b is ff
a is 00; b is ff
a is 00; b is FF