473,396 Members | 2,057 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,396 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 1699
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: 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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.