Conversion among Data Types in C++

As a Programmer, i have quite often encountered situations where i need to convert one data type to another, especially conversions between int/float to strings. Be it solving problems on online judge or during implementation of mathematical parser or data type to hold BIG INTEGERS (bigger than what a fundamental data type can hold), these kind of conversions are quite useful. Here, i discuss few methods i use to achieve it.

The most easy and flexible way to convert an information among different data types is through stringstreams which is a feature of C++. It is a part of the sstream standard library. A stringstream, as the name says, lets you manage string as if it was a stream (like the input/output stream or file stream). Here is an example showing how

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int num_int=123;
float num_float=123.456;
char num_str1[10],num_str2[10];
stringstream test(stringstream::in | stringstream::out);
test<<num_int<<endl<<num_float;    //output to the stringstream
test>>num_str1>>num_str2;    //input from the stringstream to the character arrays
cout<<num_str1<<endl<<num_str2<<endl;
return 0;
}
/*
OUTPUT:
123
123.456
*/

As you see, in the above example, the test stringstream is being treated as a normal stream and using it, the above program stores the number in the string form.
Vice versa, stringstreams can also be used to convert a number from its string form to the integer form.

Another method to achieve the conversion from string to int/float is through the atof() and atoi() functions the C standard stdlib library. atof() converts the string to float and atoi() converts it to an integer. Here is an example,

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string str_int="123";
string str_float="123.456";
int a=atoi(str_int.c_str());
float b=atof(str_float.c_str());
cout<<a<<endl<<b<<endl;
return 0;
}
/*
OUTPUT:
123
123.456
*/

Note that atoi() and atof() works only with C type string so we convert the std::string type object to C type string through the c_str() method.

To convert any int/float to string, you can use snprintf() in stdio library. It is quite similar to the stringstream method i had mentioned above. Note that i am using snprintf() instead of sprintf() as the latter can cause buffer overflow in some cases which leads to absurd results. Here is a sample code :


#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a=123;
float b=123.456;
char str_int[10],str_float[10];
snprintf(str_int,10,"%d",a);
snprintf(str_float,10,"%f",b);
cout<<str_int<<endl<<str_float<<endl;
return 0;
}
/*
OUTPUT:
123
123.45600
*/

You also have the choice to write a function that performs these jobs as the algorithms behind it are quite simple and logical.

If you got another way to perform the above jobs, they are most welcomed.

Advertisements

How did you feel about this post? Push in your reply!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s