Convert character to number in c++

Started by
11 comments, last by smart_idiot 18 years, 10 months ago
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
Advertisement
atoi works. Theres also strtoi or strtol. K.I.S.S.
william bubel
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;
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?
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]
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*.

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]

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
char text[10];
int numbers[10];
cin >> text;
for(int i=0; i<strlen(text); i++)
{
numbers = (int)text;
}
cout << numbers << endl;
return 0;
}
[smile]
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!
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]

This topic is closed to new replies.

Advertisement