Connecting Tech Pros Worldwide Help | Site Map

question on casting

John Ratliff
Guest
 
Posts: n/a
#1: Mar 14 '06
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
Phlip
Guest
 
Posts: n/a
#2: Mar 14 '06

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!!!
John Ratliff
Guest
 
Posts: n/a
#3: Mar 14 '06

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
Tomás
Guest
 
Posts: n/a
#4: Mar 14 '06

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
red floyd
Guest
 
Posts: n/a
#5: Mar 15 '06

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 (*)()).
Closed Thread