Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on casting

Reply
Thread Tools

question on casting

 
 
John Ratliff
Guest
Posts: n/a
 
      03-14-2006
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
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      03-14-2006
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!!!
 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      03-14-2006
Phlip wrote:
> 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
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-14-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Casting Performance and Pattern Question Remco ASP .Net 1 04-11-2004 05:21 AM
casting question David Eynon Java 2 07-01-2003 06:13 AM



Advertisments