Hello again! In this video, we are going to look at converting between strings and numbers.
In C, there were the functions atoi, atod and so on, for ints, doubles, and these worked with
C-style strings of character. For going the other way,
C does not provide anything, although you can use sprintf.
Or sprintf?
However you want to pronounce it!
That is a bit clumsy, so many component vendors provide non-standard functions like itoa for converting
ints to strings.
C++ does have these functions,
but now it also provides functions which work with standard strings, and these are much better. For
converting numbers to strings,
There's to underscore string, which is part of the standard library.
There are overloads which will take any numeric built-in type. And they will return the argument as a standard
string.
So, for example, if we have a floating-point number, which represents the digits of pi, this will
be converted to a string containing the characters '3', '.', '1', '4', '5', 9.
So those are actually character digits and not a numeric bit pattern. So we can try that out.
We are going to do that conversion and use that to initialize a string called pi.
Then we are going to append this string to another string, just to prove that we really have got a string
object.
So there we are.
We now have the digits of PI appended to this string, "hello".
And I notice that it's added on a zero, so obviously there is some sort of a rounding or extension going
on.
For going the other way, there's a different function for each built-in numeric type. As an example,
stoi will take a string arguments and return the int which has the same digits.
So if we have a string with the digits '4' and '2', this will convert it to an int with the value
42.
The way this works, is that it ignores leading whitespace.
So if we have some space characters at the front, those are ignored. Then it will try to convert the
following characters into a number.
If it reaches something which is not a numeric character, it will stop.
So in this case, the result would be 314.
So the space at the beginning is ignored, but this space in the middle causes the conversion to
stop.
If we want to know where the conversion stopped, then we can pass an optional second argument.
This is passed by address.
And when the function returns, it will contain the number of characters that were successfully processed.
And if the conversion was completely successful, this will be equal to the number of characters in
the string.
If it was partially successful, this will give us the index of the first character, which is not numeric.
And if the conversion is completely unsuccessful, if it can't even get started, then it will throw an
exception.
And I am afraid I have got to throw another teaser in here.
We are going to look at exceptions later on in the course.
But for now, just imagine that it means an unrecoverable error.
The function will assume that the string represents a decimal number, base 10, unless we tell it otherwise.
And if we want to use a different base, then there's another optional argument we can give.
So that is the third argument. If we are not using the second argument - if we do not care how many characters
were processed - we can just set that to null pointer.
And then this argument will be the base.
So here we are using base 16, hexadecimal, and then we are converting this hexadecimal number.
So that is 2a, which will be 42.
The base can be anything from 0 to 36.
I am not quite sure what base 0 is useful for, but it is there.
(I am sure someone must neede it) and the digits in here can be numeric digits or they can be A to Z.
So you know that in hexadecimal, we can use ABCDEF as well as the numbers.
So if you have a number in base 36, you can go all the way up to Z!
So let's try out some code.
So first of all, we're converting a string with the characters four and two into the number 42.
Then we have the string we saw in the slide, where the conversion may not succeed.
So let's see how that goes.
And then finally, we have an example of where we are converting from base 16. I have commented out the
example where it fails, for the moment.
So there we are.
We get 42, the first non-numeric character in here is at index five. So it has processed the first
two space characters and the number.
And it has got the result
314.
And then the hexadecimal conversion gave us 42. If I now uncomment this one, which is going to fail.
And, right. That has actually called abort(), so - well, we will go into that later on, but obviously the conversion
has failed!
And there are also other functions like stoll, which will convert to long long, and other
ones for unsigned long and any other built-in integer type. They all work exactly the same way.
We can also do this with floating point numbers. There is stod for double, and functions for float
and long double.
This one is almost the same, except you cannot use different bases. It all has to be decimal.
And just to illustrate the difference, if we imagine we have the string with the first few digits
of pi, "3.14159". If we use stoi, that is just going to get the integer part. It will
see the decimal point and it will say, "That is not a character."
"I am going to stop processing." With the stod, the floating-point version,
it will recognize that a dot is part of a floating point number, or it can be part of the representation
of a floating point number.
And you could have exponents as well.
And so it's going to keep on trying until it gets to the end.
Or finds a non-numeric character that cannot be part of a floating number.
So we have got our string with the digits of pi.
We are going to find out how many characters we processed, just to see how that works.
And then we have the integer version and the floating point version.
So with stoi, we get the answer three, and it has processed one character. So it processed the digit '3'.
And then it stopped, because it does not understand that dot is part of a floating-point number.
It only knows about integers. With stod, we got the full result.
So it does recognize that as a floating-point number. And it possessed all seven characters.
Okay, so that's it for this video.
I'll see you next time.
But meanwhile, keep coding!