Re: Trying to come to terms with typecasting operators
Rune Allnor wrote:
Hi all.
I need to inspect the bit patterns in a buffer. In order to do that, I
want
to print the values of each character to screen as numerical values
on
hex format.
Below is an example program that shows a number of more or less
successful attempts to achieve this. The buffer c is initialized with
the
hexadecimal numbers 0x30, 0x31, 0x32 and 0x33, which happens
to be the ASCII values of the characters '0', '1', '2' and '3'.
In attempt a) I just pipe the ASCII values to std::cout along with a
hint that I want the hex values to be printed. The attempt is clearly
unsuccessful, as the characters, not the hex values, are printed.
In attempt b) I type cast the values of the buffer from uchar to
uint. This has the desired effect, at the expense of using a
C-style type cast, which is something I want to avoid, if at all
possible.
Attempt c), where I achieve the desired result by assigning the
uchar value to be printed to the temporary uint variable d is a bit
clumsy, since it involves a temporary variable that is not strictly
necessary, either from a semantic or algorithmic point of view.
So the question is if it is possible to achieve the desired result
by using a reinterpret_cast<> or static_cast<> directly on the uchar
values inside the buffer c?
I can't see any obvious way to achieve this, since chars
have a different memory footprint than any of the int types
I am aware of, that otherwise would suit the purpose.
Are there any data types with the same memory footprint as
chars, that are interpreted by the IO library as numeric integer
values, as opposed to text glyphs?
Rune
--------------- Output ----------------
a) Naive output:
0 1 2 3
b) C-style type cast:
30 31 32 33
c) Type cast by variable assignment:
30 31 32 33
---------------------------------------
/////////////////////////////////////////////////////////////////////////////
#include <iomanip>
#include <iostream>
int main()
{
// Hex values of digits: 0 1 2 3
unsigned char c[] = {0x30,0x31,0x32,0x33};
std::cout << "a) Naive output:" << std::endl;
for (size_t n = 0; n != 4; ++n)
{
std::cout << std::hex << c[n] << " ";
}
std::cout << std::endl << std::endl;
std::cout << "b) C-style type cast:" << std::endl;
for (size_t n = 0; n != 4; ++n)
{
std::cout << std::hex << (unsigned int) c[n] << " ";
}
std::cout << std::endl << std::endl;
std::cout << "c) Type cast by variable assignment:" << std::endl;
for (size_t n = 0; n != 4; ++n)
{
size_t d = c[n];
std::cout << std::hex << d << " ";
}
std::cout << std::endl;
return 0;
}
I can't see what is wrong with the obvious:
std::cout << std::hex << static_cast<unsigned int>(c[n]) << " ";
And I don't see why you think having a "different memory footprint"
precludes a cast - a cast creates a new value which may have a
completely different "footprint" from the thing the cast is applied to.
Neil Butterworth
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]