Sign in to follow this  
Raeldor

Convert character to number in c++

Recommended Posts

What's the best way to convert a character to a number in c++? For example a char[10] to an int? Is it simply to use the atoi() function, or is there a preferred way? Thanks

Share this post


Link to post
Share on other sites
Why are you using char arrays in C++? I would highly recommend switching to C++'s std::string. If you were using std::string, you would probably do something like:

#include <sstream>
#include <string>

// ...
std::string someString;
// ... put something in someString ...
std::stringstream converter;
int someInt;
converter << someString;
converter >> someInt;

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Why are you using char arrays in C++? I would highly recommend switching to C++'s std::string. If you were using std::string, you would probably do something like:

#include <sstream>
#include <string>

// ...
std::string someString;
// ... put something in someString ...
std::stringstream converter;
int someInt;
converter << someString;
converter >> someInt;


I am programming to the windows 32 api, which using character arrays heavily. can I use the std::string with the win32 api?

Share this post


Link to post
Share on other sites
Quote:
Original post by Raeldor
Quote:
Original post by Roboguy
Why are you using char arrays in C++? I would highly recommend switching to C++'s std::string. If you were using std::string, you would probably do something like:

#include <sstream>
#include <string>

// ...
std::string someString;
// ... put something in someString ...
std::stringstream converter;
int someInt;
converter << someString;
converter >> someInt;


I am programming to the windows 32 api, which using character arrays heavily. can I use the std::string with the win32 api?


Yes. Use std::string's c_str() function. It returns a C-style string (char array) representation of the std::string. In some instances you may have to const_cast the value returned by c_str(). <- Bad advice [embarrass]

[Edited by - load_bitmap_file on June 2, 2005 9:32:08 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
In some instances you may have to const_cast the value returned by c_str().
For the love of God, NO! There is no guarantee this will work. Make a copy of the string into a local character array (using strncpy or similar), then pass the array to the function.

If the function takes a non-const char*, the array could be modified, and string::c_str() does not return a string that can be modified - which is why it returns a const char*.

Share this post


Link to post
Share on other sites
Quote:
Original post by jdhardy
Quote:
Original post by load_bitmap_file
In some instances you may have to const_cast the value returned by c_str().
For the love of God, NO! There is no guarantee this will work. Make a copy of the string into a local character array (using strncpy or similar), then pass the array to the function.

If the function takes a non-const char*, the array could be modified, and string::c_str() does not return a string that can be modified - which is why it returns a const char*.


I did it a few times and it worked so I assumed it was normal behavior. I guess not. I'd better go consult a reference on what const_cast does. [crying]

Share this post


Link to post
Share on other sites
if you want to try and see const_cast not work try this.

something like this works:

const int *ciNum = new int;
(const_cast<int*>(ciNum)) = 5;
(const_cast<int*>(ciNum)) = 6;
cout << *ciNum << '\n';

your output will be 6 but if you try this

const int Value = 25;
cout << "Value = " << Value << '\n';
int *p = const_cast<int *>(&Value);
*p = 17;
cout << "*p = " << *p << '\n';
cout << "Value = " << Value << '\n';

the int Value will not change.

Hope that helps

Also 2nd part of code is from PG 2 course at Full Saill Game Development with Arthur Johnson. A great teacher!

Share this post


Link to post
Share on other sites
Quote:
Original post by Dean Johnson
if you want to try and see const_cast not work try this.

something like this works:

const int *ciNum = new int;
(const_cast<int*>(ciNum)) = 5;
(const_cast<int*>(ciNum)) = 6;
cout << *ciNum << '\n';

your output will be 6 but if you try this

const int Value = 25;
cout << "Value = " << Value << '\n';
int *p = const_cast<int *>(&Value);
*p = 17;
cout << "*p = " << *p << '\n';
cout << "Value = " << Value << '\n';

the int Value will not change.


Quoted for emphisis, although not quite 100% accurate - the result is "undefined" if memory serves. Depending on your compiler, optimization settings, and the phase of the moon, *p = 17 will most likely either:

1) Change Value
2) Change Value, with many/most/all locations where it is directly used not reflecting those changes. Example:
const int value = 26;
const int * pointer = &value;
...
*(const_cast< int * >( &value )) = 15;
...
cout << value << " " << *pointer << endl; //may print "26 15"

3) Segfault (the Windows term for this is "Access Violation")
4) Launch nuclear missiles at a cow ranch in Alaska


const_cast should only be used to deal with const-broken libraries - using it to modify the contents of a std::string is just plain stupid. I mean that. If your standard library implementation uses COW you could very easily end up modifying more than 1 string at a single time, and have a potentially very hard to trace down bug on your hands, which could get you fired.

Here is probably the best way to deal with this kind of situation, pretending that our compiler is the only spawn of cithlu and diallows using operator+ with std::strings, or just about any decent C++ functionality related to dealing with ranges of stuff, forcing us to use strcpy and strcat:

std::string pie = "I like pie oh yes I do...";
std::string pie_append = "\nYummy oh yummy oh yummy old pie...";

std::vector< char > pie_data( pie.size() + pie_append.size() + 1 );
strcpy( &(pie_data[0]) , pie.c_str() );
strcat( &(pie_data[0]) , pie_append.c_str() );
pie = &(pie_data[0]);




Note: pie += pie_append will be faster unless your compiler is from 1990*. See details here.

* approximation, not liable from hot coffee spilled in one's lap out of suprise if this is slower and your compiler is from Jan 1st, 1991.

Edit: You know you're tired when you try and wrap your disclaime with <size font="-2"> and </size>. Fixed.

[Edited by - MaulingMonkey on June 3, 2005 4:49:37 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
char c = '4';
int i = c - '0';
cout <


Nice. Reminds me of the old converting decimal to hex by indexing into an array of "1234567890ABCDEF".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this