cast safe



 DEVELOP > c-Plus-Plus > cast safe

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "grahamo"
Date: 14 Dec 2004 08:18:48 AM
Object: cast safe
Hi,
i know I should use the new style casts and I intend to however I
would like to know how I go about this;
I have an unsigned char* that a 3rd party API returned to me. The API
reads text from a file and gives me that test as an unsigned char*.
For the sake of this example the text string is "100"
unsigned char* data = foo();
I need to get this into an unsigned int, which is what it ultimately
should be however I'm not sure of the best approach. I can cast it to
a char* and then use sprintf(result, "%d", arg) but I'm not sure if
thats the best approach.
What's the *correct* way to achieve it... in terms of code correctness
and "correct approach".
Thanks much for any info (as usual:)
GrahamO
.

User: "Mike Wahler"

Title: Re: cast safe 16 Dec 2004 02:59:34 PM
"grahamo" <graham_walsh50@hotmail.com> wrote in message
news:79528aa8.0412140618.fe964cc@posting.google.com...

Hi,

i know I should use the new style casts and I intend to however I
would like to know how I go about this;

I have an unsigned char* that a 3rd party API returned to me. The API
reads text from a file and gives me that test as an unsigned char*.
For the sake of this example the text string is "100"

unsigned char* data = foo();

I need to get this into an unsigned int, which is what it ultimately
should be however I'm not sure of the best approach. I can cast it to
a char* and then use sprintf(result, "%d", arg) but I'm not sure if
thats the best approach.

What's the *correct* way to achieve it... in terms of code correctness
and "correct approach".

Thanks much for any info (as usual:)

There are many ways which are 'correct'. The 'best'
depends upon your needs. E.g.:
std::istringstream iss(static_cast<char>(foo()));
int i(0);
if(!(iss >> i))
std::cerr << "Cannot convert text to integer\n";
else
/* do your thing */
If I wanted more detailed control and error detection
for the conversion, I might do something like this:
std::string s(static_cast<char>(foo()));
long tmp(0);
tmp = strtol(s.c_str(), /* etc */ );
/* (look up 'strtol()' for the other */
/* parameters and how to use it) */
int i(0);
if(tmp >= std::numeric_limits<int>::min() &&
tmp <= std::numeric_limits<int>::max())
i = static_cast<int>(tmp);
else
std::cerr << "value out of range for int\n";
Also, if 'foo()'s return value can be NULL, you need to check for
that before using it.
-Mike
.

User: "Victor Bazarov"

Title: Re: cast safe 14 Dec 2004 08:48:58 AM
grahamo wrote:

i know I should use the new style casts and I intend to however I
would like to know how I go about this;

I have an unsigned char* that a 3rd party API returned to me. The API
reads text from a file and gives me that test as an unsigned char*.
For the sake of this example the text string is "100"

unsigned char* data = foo();

I need to get this into an unsigned int, which is what it ultimately
should be however I'm not sure of the best approach. I can cast it to
a char* and then use sprintf(result, "%d", arg) but I'm not sure if
thats the best approach.

It's definitely not the best approach because you probably need 'sscanf'
and not 'sprintf'.

What's the *correct* way to achieve it... in terms of code correctness
and "correct approach".

You can roll your own conversion routine that would take unsigned char*
instead of char*, but in most cases you may cast unsigned char* to char*
without any problem, and then use 'strtol'.
Victor
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER