Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 14th, 2006, 10:55 PM
John Ratliff
Guest
 
Posts: n/a
Default question on casting

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
  #2  
Old March 14th, 2006, 11:15 PM
Phlip
Guest
 
Posts: n/a
Default Re: question on casting

John Ratliff wrote:
[color=blue]
> 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?[/color]

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!!!
  #3  
Old March 14th, 2006, 11:15 PM
John Ratliff
Guest
 
Posts: n/a
Default Re: question on casting

Phlip wrote:[color=blue]
> John Ratliff wrote:
>
>[color=green]
>>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?[/color]
>
>
> 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.
>[/color]

Thanks,

--John Ratliff
  #4  
Old March 14th, 2006, 11:55 PM
Tomás
Guest
 
Posts: n/a
Default Re: question on casting

John Ratliff posted:
[color=blue]
> 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[/color]


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
  #5  
Old March 15th, 2006, 05:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: question on casting

Tomás wrote:[color=blue]
>
> 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.
>[/color]


From what I've seen, there are still a few instances where a C-style
cast is required. The main one being a cast from ptr-to-function to
void* (I needed to print the value of a void (*)()).
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles