John Ratliff wrote:
What is the proper way to cast charater pointers to and from unsigned?
For example, say I had a heap allocated char *ptr;
char *ptr = new char[0x2000];
and I wanted to convert it to an unsigned char *uptr;
unsigned char *uptr = (unsigned char *)ptr; // C-style works
unsigned char *uptr = static_cast<unsigned char *>(ptr);
// static_cast doesn't
unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
// reinterpret_cast works, but is this correct?
You are correct to not want to use a C-style cast, and to try to use the
cast closest to static_cast.
With a few minor exceptions, every C-style cast has at least one equivalent
elaborate_cast. (The exceptions are dynamic_cast, which is new, and
const_cast, which might be needed to assist another cast to de-qualify a
type and match a sloppy C-style cast).
So, yes, unsigned chars are not signed chars, just as the class SimCity is
not a std::string. So copying the bits out of one and jamming them into
another is a reinterpretation, and reinterpret_cast is needed.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!