473,387 Members | 1,502 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,387 software developers and data experts.

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
Jul 22 '05 #1
2 1697
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
Jul 22 '05 #2

"grahamo" <gr************@hotmail.com> wrote in message
news:79*************************@posting.google.co m...
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
Jul 22 '05 #3

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

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
2
by: wenmang | last post by:
Hi, all: I try to understand whether it is a portable to cast a pointer type, e.g., char *ptr = char array; -- a buffer unsigned char *uptr = ptr; : : function(uptr); -- execute a function...
31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
1
by: grahamo | last post by:
Doh, In my last mail I meant the function atoi instead of sprintf... and I also meant to use an int instead of an unsigned int......... this is the approach I mean , ignore the other...
5
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
6
by: Nick Weekes | last post by:
Hi all, I am trying to cast a Double (its actually the NextDouble method of the Random class, but could be any type of number) to a Type via its name. Im trying the following syntax: ...
2
by: lovecreatesbea... | last post by:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In section 16.3.2, the book says that variables type struct sockaddr_in; and/or struct sockaddr_in6; will be cast explicitly to...
5
by: christian.pontesegger | last post by:
Hi all, lately I had a problem where I wanted to cast a basic class to a derived type, which did not introduce any new members and did not change any stuff with dynamically allocated memory. I...
9
by: jason.cipriani | last post by:
All right, I'm in this weird situation that's hard to explain but I've put together a small example program that represents it. Please bear with the fact that some of the stuff in the example seems...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.