PDA

View Full Version : strings/numbers conversion in C++


bee
28th September 2008, 05:04 PM
Hi!!! :cool:

How do i convert a string to one number in C++?
Like, in C:
int n = atoi("15");
unsigned int n = atoi("15"); [?]
long n = atol("150");
unsigned long n = atol("150"); [?]
long long n = ato??("150"); [??]
unsigned long long n = ato??("150"); [??]

Somebody know how to do it in C++? plus, i've really no ideas about that "long long" conversion.

bye! :)

sideways
28th September 2008, 07:52 PM
the Boost Library (http://en.wikipedia.org/wiki/Boost_C%2B%2B_Libraries) is good for this and lots of other stuff

// test1.cpp

#include <iostream>
#include <boost/lexical_cast.hpp>

using boost::lexical_cast;
using boost::bad_lexical_cast;

int main()
{
int i;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
char* str_i = "2147483647";
char* str_l = "2147483647";
char* str_ul = "4294967295";
char* str_ll = "9223372036854775807";
char* str_ull = "18446744073709551615";

try { i = lexical_cast<int>(str_i); }
catch(bad_lexical_cast &) { i = 0; }

try { l = lexical_cast<long>(str_l); }
catch(bad_lexical_cast &) { l = 0; }

try { ul = lexical_cast<unsigned long>(str_ul); }
catch(bad_lexical_cast &) { ul = 0; }

try { ll = lexical_cast<long long>(str_ll); }
catch(bad_lexical_cast &) { ll = 0; }

try { ull = lexical_cast<unsigned long long>(str_ull); }
catch(bad_lexical_cast &) { ull = 0; }

std::cout << "i = " << i << std::endl;
std::cout << "l = " << l << std::endl;
std::cout << "ul = " << ul << std::endl;
std::cout << "ll = " << ll << std::endl;
std::cout << "ull = " << ull << std::endl;

}

[user@localhost ~]$ sudo yum install boost-devel
[user@localhost ~]$ g++ -o test1 test1.cpp
[user@localhost ~]$ ./test1
i = 2147483647
l = 2147483647
ul = 4294967295
ll = 9223372036854775807
ull = 18446744073709551615
[user@localhost ~]$

edit:
int = long = signed 32bit, long long = signed 64bit
[user@localhost ~]# bc <<< "2^31; 2^32; 2^63; 2^64"
2147483648
4294967296
9223372036854775808
18446744073709551616

bee
28th September 2008, 09:22 PM

hi thank you! it really work! :) :D
Though i've edit:
try { num = boost::lexical_cast<unsigned long long>(number_var); }
catch(boost::bad_lexical_cast &) { num = 0; }to avoid to write the two "using".
Well, yes with this libs it work.
But i wonder, how there are no ways to convert strings to numbers without using any of the standard libs?

thanks! bye! :)

sideways
28th September 2008, 10:31 PM
You can use the standard libs, but it's a little more complex to get the types correct. The Boost library makes it all a bit easier (you can also convert float and double types using lexical_cast).

If you want to do it using standard libs, here's the first example I found googling "c++ convert string to numeric"

http://www.codeguru.com/forum/showthread.php?t=231054

sideways
29th September 2008, 10:05 AM
I had another look at the standard libs and it's simpler than I remembered, you use std:istringstream to do the conversion, and create a template class to mimic lexical_cast.

// test3.cpp

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

template <class T>
bool numeric_cast(T& t, const std::string& s)
{
std::istringstream iss(s);
return !(iss >> std::dec >> t).fail();
}

int main()
{
int i;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
char* str_i = "2147483647";
char* str_l = "2147483647";
char* str_ul = "4294967295";
char* str_ll = "9223372036854775807";
char* str_ull = "18446744073709551615";

float f;
double d;
long double ld;
char* str_f = "123.456789";
char* str_d = "123.4567890123";
char* str_ld = "123.4567890123456789";

if ( !numeric_cast<int>(i, str_i) ) i = 0;
if ( !numeric_cast<long>(l, str_l) ) l = 0;
if ( !numeric_cast<unsigned long>(ul, str_ul) ) ul = 0;
if ( !numeric_cast<long long>(ll, str_ll) ) ll = 0;
if ( !numeric_cast<unsigned long long>(ull, str_ull) ) ull = 0;

if ( !numeric_cast<float>(f, str_f) ) f = 0.0;
if ( !numeric_cast<double>(d, str_d) ) d = 0.0;
if ( !numeric_cast<long double>(ld, str_ld) ) ld = 0.0;

std::cout << "i = " << i << std::endl;
std::cout << "l = " << l << std::endl;
std::cout << "ul = " << ul << std::endl;
std::cout << "ll = " << ll << std::endl;
std::cout << "ull = " << ull << std::endl;

std::cout << std::setprecision(8) << "f = " << f << std::endl;
std::cout << std::setprecision(16) << "d = " << d << std::endl;
std::cout << std::setprecision(20) << "ld = " << ld << std::endl;
}


[user@localhost ~]$ g++ -o test3 test3.cpp
[user@localhost ~]$ ./test3
i = 2147483647
l = 2147483647
ul = 4294967295
ll = 9223372036854775807
ull = 18446744073709551615
f = 123.45679
d = 123.4567890123
ld = 123.4567890123456789


lexical_cast does do bi-directional conversion however, and is simpler to use overall http://monzool.net/blog/category/programming/c/

bee
29th September 2008, 12:54 PM
You can use the standard libs, but it's a little more complex to get the types correct. The Boost library makes it all a bit easier (you can also convert float and double types using lexical_cast).

If you want to do it using standard libs, here's the first example I found googling "c++ convert string to numeric"

http://www.codeguru.com/forum/showthread.php?t=231054
thank you!!
i've found that message before too!, but i don't have understand how it works, now i've got it a bit more; i forget to copy that numeric_cast class the first time :D:p

So, now i know that C++ is missing of a good simple way for conversions (and it sounds so bad..), but with boost it's simple to do.
Boost documentation is hard on their website, if you know can you tell me where can i find more examples? so i can learn from code. I have got the examples in boost, but they are not for all libs i think. I can use google and read articles, but what i really need is a zip file with example-sources. It's more simple and quick to learn for me.

thank you again!! bye!:)

sideways
29th September 2008, 01:29 PM
I would spend an afternoon reading through links from this page
http://www.deitel.com/ResourceCenters/Programming/CPlusPlusBoostLibraries/tabid/1893/Default.aspx

Or you can read Beyond the C++ Standard Library: An Introduction to Boost (http://safari.oreilly.com/0321133544) online (full text requires safari registration, but you do get a free 10 day trial)

Aaron222
22nd October 2008, 02:58 AM
sstream s ;
int i = 50;
s << i;
string a = "abc";
a = a+s.str();

I hope it helps...

Linux Archive (http://www.linux-archive.org/)

fedoraman08
22nd October 2008, 03:06 AM
the Boost Library (http://en.wikipedia.org/wiki/Boost_C%2B%2B_Libraries) is good for this and lots of other stuff

// test1.cpp

#include <iostream>
#include <boost/lexical_cast.hpp>

using boost::lexical_cast;
using boost::bad_lexical_cast;

int main()
{
int i;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
char* str_i = "2147483647";
char* str_l = "2147483647";
char* str_ul = "4294967295";
char* str_ll = "9223372036854775807";
char* str_ull = "18446744073709551615";

try { i = lexical_cast<int>(str_i); }
catch(bad_lexical_cast &) { i = 0; }

try { l = lexical_cast<long>(str_l); }
catch(bad_lexical_cast &) { l = 0; }

try { ul = lexical_cast<unsigned long>(str_ul); }
catch(bad_lexical_cast &) { ul = 0; }

try { ll = lexical_cast<long long>(str_ll); }
catch(bad_lexical_cast &) { ll = 0; }

try { ull = lexical_cast<unsigned long long>(str_ull); }
catch(bad_lexical_cast &) { ull = 0; }

std::cout << "i = " << i << std::endl;
std::cout << "l = " << l << std::endl;
std::cout << "ul = " << ul << std::endl;
std::cout << "ll = " << ll << std::endl;
std::cout << "ull = " << ull << std::endl;

}

[user@localhost ~]$ sudo yum install boost-devel
[user@localhost ~]$ g++ -o test1 test1.cpp
[user@localhost ~]$ ./test1
i = 2147483647
l = 2147483647
ul = 4294967295
ll = 9223372036854775807
ull = 18446744073709551615
[user@localhost ~]$

edit:
int = long = signed 32bit, long long = signed 64bit
[user@localhost ~]# bc <<< "2^31; 2^32; 2^63; 2^64"
2147483648
4294967296
9223372036854775808
18446744073709551616

Thanks, It help solving my problem too