C++ Tutorial for noobs - Part 11: Type-Conversions
Motivation
In the previous parts we talked a lot about all the different data-types that C++ has to offer. In this part of our C++ tutorial series we will learn about how to convert one data-type to another. What sounds simple is often a difficult task that requires us to create our own functions for it.
In case it's not clear yet: the question is how we can add the number 42 to the string "abc", or how we can convert the string "123" to the number 123 and so on.
C++ Float to Int
Code
int float2int(float x)
{
return (int)(x + 0.5f);
}
Explanation
There are two ways to convert a float value to an int value. The easy way would be to just cast it to int via (int)x, which simply cuts off everything behind the decimal point. If we think remember school math, we learned that we still have to round the number, which means that if the stuff behind the decimal point is bigger than 0.5 it will be rounded to the smaller, otherwise to the bigger number. This is the reason for + 0.5f in the above code.
Don't worry if you don't understand it immediately, just play around with it and you will see that it really works.
Example
int i = float2int(12.34f); // gives 12
C++ Int to Float
Explanation
Now this one is really easy. C++ converts int values to float values automatically, there is nothing for us to worry about.
Example
int i = 42;
float f = i;
C++ Int to String
Code
#include <sstream>
#include <string>
std::string int2str(int x)
{
std::stringstream ss; // create a stringstream
ss << x; // add number to the stream
return ss.str(); // return stream content
}
Explanation
The function creates a C++ stringstream, which is like a big list of many different types. It then puts the int value into it and asks the stringstream to return everything that's currently inside of it as a C++ string.
Example
std::string s = "abc" + int2str(42);
// gives "abc42"
C++ Float to String
Code
#include <sstream>
#include <string>
std::string float2str(float x)
{
std::stringstream ss; // create a stringstream
ss << x; // add number to the stream
return ss.str(); // return stream content
}
Explanation
The function creates a C++ stringstream again, puts the float value into it and asks the stringstream to return everything that's currently inside of it as a C++ string.
Example
std::string s = "abc" + float2str(12.34f);
// gives "abc12.34"
C++ String to Int
Code
#include <sstream>
#include <string>
int str2int(std::string s)
{
int number;
std::stringstream ss;
ss << s;
ss >> number;
return number;
}
Explanation
To convert a string to an int, we can use the C++ stringstream again. This time we just put the string into it with << and then read a int number out of it with >>.
Example
int i = str2int("42");
C++ String to Float
Code
float str2float(std::string s)
{
float number;
std::stringstream ss;
ss << s;
ss >> number;
return number;
}
Explanation
To convert a string to a float value, we can use the C++ stringstream yet again. This time we just put the string into it with << and then read a float number out of it with >>.
Example
float f = str2float("12.34");