In this lecture, we're going to see how we can transform back and forth between strings and numbers.
And this is a common operation in your career as a C plus plus developer.
We're going to see some of the facilities.
We have to do these kinds of tasks.
Okay.
We're going to see how we can go from number to string.
And this happens if you happen to have a number stored in some variable and you want to treat it like
a string.
For that, we can use the STD to string facility here and we're going to give it whatever we want to
turn into a string in this parentheses.
And what we're going to get out of that is basically going to be a string we can pass in floats, we
can pass in doubles, we can pass in integers, basically any number type you have in C plus plus,
you can pass it to this thing and it's going to turn that back into a string.
So here we have this.
Things transform.
So down here we are just trying to print them out to see that the transformation was actually done.
And we are going to try this out in Visual Studio code in a minute.
So STD to string is what you use to go from number to STD string.
But sometimes we want to do the reverse.
We want to go from a string to a number.
For example, if somebody typed a number on the console, if our application is taking input from the
user, we're going to get that input as a string.
So we might want to turn that into a number type, for example, float to double or ENT so that we can
work with it in our program as a number type.
There are a few facilities we can use from the C plus plus standard library.
And here is a screenshot I took from the documentation from CPP reference for STD string.
If you go to the right here, you're going to see a title that says numeric conversions and we have
a few things we can use to transform back and forth between numbers and strings.
We have seen two strings.
This is what we just saw, but now we are interested in going from a string to a number and we're going
to be using these little guys in here s to i.
S to L.
S to l l.
S to L And we're going to basically see what they do.
What we have on the left here is going to be working on integer types.
We have integer, we have long and we have long, long and we have unsigned types for integers.
So we can use unsigned long and unsigned, long, long.
We can do all these kinds of crazy things.
If we want to go to a floating type, we have a few we can use here.
To the right we have string to float, string to double string to long double, and we can use these
things.
So what we're going to do in the next few slides is to look at a few examples of these things and see
how they work.
Really.
Here you see how you can transform from a string to an integral type like integer, long int or long,
long int Stdstring number.
Here is a variable that is going to hold our input string.
We can put in a representation of a number, but this is going to be a string and notice that it is
between double quotes.
So this is going to be a string literal.
So we are using to initialize our stdstring variable here.
We can turn this into an integer, for example, by using to I here we are going to give it our input
value, which is going to be a string representing the number and we're going to do the transformation
here and turn that into an integer.
After this operation, we're going to have this integral type stored in into Var here and we can use
it however we want.
For example, we can print var, we can see the size of it and really be sure that it is an integer.
We can also do the same operations for long type.
So this is a variable of long type.
We're going to say S to L, we're going to give it the input and out of this we're going to get a long
variable.
We can do the same for long, long.
It is basically the same thing and we can print these things out.
And I am just trying to raise your awareness on these facilities you have.
If you happen to want to transform between strings and numbers, we also have versions that are going
to work on unsigned integral types.
So we have STL, which is going to give us an unsigned long type and we have S2L, which is going to
give us a long time unsigned type.
This should be H2O, but we're going to correct that.
When we hit Visual Studio code, we also have versions that are going to give us floating types and
these are respectively S2F, which is going to give us a float S2D, which is going to give us a double
and S2D, which is going to give us a long double.
And we are printing these things out to see what we get out of them.
Okay?
This is really all about these methods.
Now we are going to head over to Visual Studio code and actually play with them.
Okay.
Here we are in our working folder.
The current project is transforming from string to numbers and we are going to put in our template file.
So let's do that.
I'm going to copy them and put them in place and we are going to open this up in Visual Studio code.
We're going to open this and we are going to.
Food, our string library, and we are going to put in the code because we want to try and turn numbers
into strings.
We are going to use STD to string here so we can use that.
We have a few number types float double and int and we want to turn these guys into strings.
If you go down here, we set up a few strings float string, double string and int string and we are
going to initialize these guys with the return of STD two string here.
So STD two string is what is going to turn a number into a string.
And the first line here, what we are doing, we are taking a float variable.
We are turning that into a string and we are using the result of that to initialize our float string
variable.
And we are basically doing the same thing for double string and int string here.
And after we do this, our strings are going to contain string representations of these numbers we have
on top here.
So down here we are just trying to print these things to show that it is exactly what we want.
We're going to get the size of our float string too, just to see how it looks.
We're going to print the variable and we're going to print the string representation of it.
And we do the same thing here for double var and int for let's try and build this with GCC.
Let's give ourselves some breathing room and we are going to build with GCC, we're going to use the
task to do that.
The word is good.
We're going to clear and if we run Rooster, we're going to say size of float string is nine.
And let's look at what Float String is.
Here it is down here and we have 22 .299999.
Many nines.
If we count how many characters we have in here, we're going to have one, two, three, four, five,
six, seven, eight, nine characters.
And that's what we get in here.
This is a string.
It's not a number.
We also have a float of are printed out.
This is a float variable.
We have double Var printed and we have it's a string representation.
If we print the size, we're going to get a larger size down at the nine we have here.
You can try and do that.
We have the integer and we have a string representation which is also a two, two, three.
So this is how you can transform numbers into strings.
We are going to comment this out because we want to also see how we can do the reverse transformation
and go from strength to number.
We are going to go down and put in a piece of code that is going to allow us to play with us.
What we want is to go from a strength to a number, and the facilities we're going to be using are S
to I, which is string to integer S to L, which is string too long and S to ll, which is string to
long long.
So we have our input string which is a representation of a number and we're going to turn that into
an integer using s to I, we're going to turn that into a long var using s to L and we're going to turn
that into a long, long integer using S and we can print these guys out.
You notice that this guy is negative.
So this could even be used for negative numbers and it is really cool.
So let's try and build this.
We're going to use GCC and the build is good.
We can clear and run Rooster.
We're going to see that into Var is -34.
Size of that is four because it is an integer long var is going to be -34.
Size of long var is going to be four and long long.
The value inside is -34, the size of it is eight.
So we have these values in here from a string and we are using this facilities here is to i.
S to L and s to l l.
You can use these guys to turn whatever string you have into a number, but make sure it really is a
string that makes sense to transform into a number.
For example, you can't take a, B, C like this and expect to get something useful out of this.
If you're trying to build this, let's see what we get.
The build is going to be successful, but you are most probably going to get some garbage.
So let's look at what we have.
It's going to crash because you really are trying to do something you shouldn't do.
Okay?
So don't do that.
And you see that this guy was throwing an exception, which is going to terminate the program if it
is not handled.
We haven't really learned about exceptions yet, but so far, just know that you can't pass something
that doesn't make sense to be turned into a number.
Just put in something that looks like a number.
For example, we can put in a positive number here and if we build, we're going to get something reasonable.
Let's clear.
And we are going to build.
The world is good.
And if we run Rooster, we are going to get our values in here.
And this is really cool.
Okay, This is how you can take a string and turn that into an integral type.
We are going to see how we can get some unsigned integral types next.
Let's comment this out.
Notice that we want to get unsigned types, so we need to really take a string that can be turned into
an unsigned type.
Again, make sure that the input is something that makes sense to be turned into an unsigned type.
Okay.
If you put in something bad, you're going to get some garbage output or even worse, your program is
going to crash.
So here we have our number string.
We're going to turn that into unsigned long using this facility here, or we can turn this into unsigned,
long, long.
So let's do that.
H2ULL That's what we should do here.
This was a typo.
So if we run this, we're going to get this.
Thanks.
Run and give us our integral types and we can use them however we want.
This world we're going to build with GCC.
And let's clear this out.
The world is good and we're going to run, Rooster.
We're going to have our values in here.
So unsigned long var is 34.
Size of it is four.
You see that?
It is exactly what we want.
Unsigned long, long var is 34 and the size of it is eight.
And this is exactly what we expect.
So if you want to take a string and turn that into an unsigned integral type, these guys are going
to be your friends.
But before we go from this, let's see what could happen if you try to put in a negative number.
If you try to do that.
We are trying to get an unsigned long integer, but we really are putting in a string representation
of a negative number.
So you can expect this to blow up.
Okay, so let's try and build this.
We're going to build with GCC.
Let's see if the world even goes through, the world is going to go through and we are going to get
something bad happen if we try to print these guys, Look at what we have in here, unsigned long var.
It's really something we didn't put in.
This is definitely not -34.567.
And the reason is the compiler is going to just underflow because this is a negative number.
We are trying to store that into an unsigned integer.
We're going to get an underflow and this is going to flip to a really large number for that floating
type.
So that's what we are getting here.
But be sure you really put in what you think is going to give you a valid number.
If you try to put in something crazy, you're going to get garbage output like this here, or even worse,
you're going to get a crash.
So don't do that.
We are going to comment this out and see how we can turn our strings into floating types.
Let's go down and do that.
Okay.
What we want is to turn a string into a floating type.
That's what we say here.
And the facilities we're going to use are S2F, s, two D and S2D, which are respectively going to
give us a float, a double and a long double type.
We have our input string here and we're going to pass this into this little facilities we have and we're
going to be getting our number types in here.
Float value is going to be a representation of this guy in floating type double.
Var is going to be a double version of this and a long double var is going to be a long double version
of this.
Let's try and build this and see that it goes through.
We are going to use GCC.
As always, the world is good.
We're going to clear and run roster and we have our numbers here.
One is a float, the other is a double and the other is long double.
And this is what we expect.
This is really all we set out to do in this lecture showing you how you can go back and forth between
string and numbers.
And these are the facilities you can use for these purposes.
We are going to stop here in this lecture and the next one we're going to learn about row string literals.
Go ahead and finish up here and meet me there.