question on casting

J

John Ratliff

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?

Thanks,

--John Ratliff
 
P

Phlip

John said:
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.
 
J

John Ratliff

Phlip said:
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.

Thanks,

--John Ratliff
 
T

Tomás

John Ratliff posted:
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?

Thanks,

--John Ratliff


unsigned char* Convert(char* p)
{
return reinterpret_cast< unsigned char* >(p);
}

Here's my method, it's not very scientific but it does the trick:

1) Try use static_cast.

If it doesn't compile then:

2) Try use reinterpret_cast

If it _still_ doesn't compile, then don't hesitate to use dirty methods.


-Tomás
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top