473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Mar 14 '06 #1
4 1840
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!!!
Mar 14 '06 #2
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
Mar 14 '06 #3
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
Mar 14 '06 #4
Tomás wrote:

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.

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 (*)()).
Mar 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
26
by: Method Man | last post by:
Say I have the following: int main(void) { char* p, q; p = (char*) malloc(sizeof(char)*10); q = (p + 100) - 99; /* legal? */ free(q - 1); /* legal? */ .... return 0; }
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
14
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.