WEBVTT

00:01.500 --> 00:06.600
Hello again! In this video, we are going to look at converting between strings and numbers.

00:08.480 --> 00:14.900
In C, there were the functions atoi, atod and so on, for ints, doubles, and these worked with

00:14.900 --> 00:19.400
C-style strings of character. For going the other way,

00:19.400 --> 00:22.640
C does not provide anything, although you can use sprintf.

00:23.630 --> 00:24.520
Or sprintf?

00:24.900 --> 00:25.940
However you want to pronounce it!

00:26.570 --> 00:32.840
That is a bit clumsy, so many component vendors provide non-standard functions like itoa for converting

00:33.110 --> 00:34.670
ints to strings.

00:36.350 --> 00:38.210
C++ does have these functions,

00:38.580 --> 00:45.230
but now it also provides functions which work with standard strings, and these are much better. For

00:45.230 --> 00:47.140
converting numbers to strings,

00:47.150 --> 00:50.770
There's to underscore string, which is part of the standard library.

00:51.410 --> 00:57.860
There are overloads which will take any numeric built-in type. And they will return the argument as a standard

00:57.920 --> 00:58.340
string.

00:59.690 --> 01:06.380
So, for example, if we have a floating-point number, which represents the digits of pi, this will

01:06.380 --> 01:11.150
be converted to a string containing the characters '3', '.', '1', '4', '5', 9.

01:11.360 --> 01:17.680
So those are actually character digits and not a numeric bit pattern. So we can try that out.

01:18.020 --> 01:22.520
We are going to do that conversion and use that to initialize a string called pi.

01:23.180 --> 01:27.560
Then we are going to append this string to another string, just to prove that we really have got a string

01:27.560 --> 01:27.980
object.

01:30.940 --> 01:31.600
So there we are.

01:31.630 --> 01:36.040
We now have the digits of PI appended to this string, "hello".

01:36.520 --> 01:43.390
And I notice that it's added on a zero, so obviously there is some sort of a rounding or extension going

01:43.390 --> 01:43.570
on.

01:50.260 --> 01:56.050
For going the other way, there's a different function for each built-in numeric type. As an example,

01:56.050 --> 02:02.080
stoi will take a string arguments and return the int which has the same digits.

02:03.190 --> 02:09.040
So if we have a string with the digits '4' and '2', this will convert it to an int with the value

02:09.040 --> 02:09.700
42.

02:11.230 --> 02:14.690
The way this works, is that it ignores leading whitespace.

02:14.710 --> 02:21.670
So if we have some space characters at the front, those are ignored. Then it will try to convert the

02:21.670 --> 02:24.070
following characters into a number.

02:25.670 --> 02:29.390
If it reaches something which is not a numeric character, it will stop.

02:29.870 --> 02:33.020
So in this case, the result would be 314.

02:34.880 --> 02:39.140
So the space at the beginning is ignored, but this space in the middle causes the conversion to

02:39.140 --> 02:39.470
stop.

02:44.740 --> 02:49.840
If we want to know where the conversion stopped, then we can pass an optional second argument.

02:50.320 --> 02:52.060
This is passed by address.

02:53.220 --> 02:58.680
And when the function returns, it will contain the number of characters that were successfully processed.

02:59.700 --> 03:05.280
And if the conversion was completely successful, this will be equal to the number of characters in

03:05.280 --> 03:05.730
the string.

03:06.960 --> 03:13.050
If it was partially successful, this will give us the index of the first character, which is not numeric.

03:14.350 --> 03:18.940
And if the conversion is completely unsuccessful, if it can't even get started, then it will throw an

03:18.940 --> 03:19.450
exception.

03:20.320 --> 03:22.900
And I am afraid I have got to throw another teaser in here.

03:22.930 --> 03:25.150
We are going to look at exceptions later on in the course.

03:25.990 --> 03:28.810
But for now, just imagine that it means an unrecoverable error.

03:30.300 --> 03:36.270
The function will assume that the string represents a decimal number, base 10, unless we tell it otherwise.

03:36.660 --> 03:41.040
And if we want to use a different base, then there's another optional argument we can give.

03:42.210 --> 03:47.130
So that is the third argument. If we are not using the second argument - if we do not care how many characters

03:47.130 --> 03:49.500
were processed - we can just set that to null pointer.

03:50.550 --> 03:52.110
And then this argument will be the base.

03:52.410 --> 03:59.010
So here we are using base 16, hexadecimal, and then we are converting this hexadecimal number.

03:59.850 --> 04:02.580
So that is 2a, which will be 42.

04:04.080 --> 04:07.080
The base can be anything from 0 to 36.

04:07.620 --> 04:11.510
I am not quite sure what base 0 is useful for, but it is there.

04:12.420 --> 04:17.520
(I am sure someone must neede it) and the digits in here can be numeric digits or they can be A to Z.

04:18.510 --> 04:23.160
So you know that in hexadecimal, we can use ABCDEF as well as the numbers.

04:23.820 --> 04:27.330
So if you have a number in base 36, you can go all the way up to Z!

04:30.690 --> 04:32.430
So let's try out some code.

04:32.820 --> 04:38.250
So first of all, we're converting a string with the characters four and two into the number 42.

04:39.210 --> 04:45.060
Then we have the string we saw in the slide, where the conversion may not succeed.

04:45.060 --> 04:46.140
So let's see how that goes.

04:47.790 --> 04:53.190
And then finally, we have an example of where we are converting from base 16. I have commented out the

04:53.670 --> 04:55.560
example where it fails, for the moment.

04:59.040 --> 04:59.880
So there we are.

05:00.330 --> 05:09.900
We get 42, the first non-numeric character in here is at index five. So it has processed the first

05:09.900 --> 05:12.300
two space characters and the number.

05:13.430 --> 05:14.400
And it has got the result

05:14.470 --> 05:15.230
314.

05:15.920 --> 05:23.450
And then the hexadecimal conversion gave us 42. If I now uncomment this one, which is going to fail.

05:26.210 --> 05:33.110
And, right. That has actually called abort(), so - well, we will go into that later on, but obviously the conversion

05:33.110 --> 05:33.620
has failed!

05:39.470 --> 05:45.470
And there are also other functions like stoll, which will convert to long long, and other

05:45.470 --> 05:50.750
ones for unsigned long and any other built-in integer type. They all work exactly the same way.

05:52.380 --> 05:57.450
We can also do this with floating point numbers. There is stod for double, and functions for float

05:57.450 --> 05:58.470
and long double.

05:59.630 --> 06:03.950
This one is almost the same, except you cannot use different bases. It all has to be decimal.

06:06.300 --> 06:11.790
And just to illustrate the difference, if we imagine we have the string with the first few digits

06:11.790 --> 06:18.210
of pi, "3.14159". If we use stoi, that is just going to get the integer part. It will

06:18.210 --> 06:21.630
see the decimal point and it will say, "That is not a character."

06:22.080 --> 06:26.730
"I am going to stop processing." With the stod, the floating-point version,

06:27.120 --> 06:33.180
it will recognize that a dot is part of a floating point number, or it can be part of the representation

06:33.180 --> 06:34.410
of a floating point number.

06:35.070 --> 06:36.750
And you could have exponents as well.

06:37.320 --> 06:40.530
And so it's going to keep on trying until it gets to the end.

06:40.530 --> 06:44.100
Or finds a non-numeric character that cannot be part of a floating number.

06:47.530 --> 06:49.840
So we have got our string with the digits of pi.

06:50.740 --> 06:55.390
We are going to find out how many characters we processed, just to see how that works.

06:55.960 --> 06:58.750
And then we have the integer version and the floating point version.

07:01.990 --> 07:08.980
So with stoi, we get the answer three, and it has processed one character. So it processed the digit '3'.

07:09.310 --> 07:14.860
And then it stopped, because it does not understand that dot is part of a floating-point number.

07:15.070 --> 07:19.990
It only knows about integers. With stod, we got the full result.

07:20.530 --> 07:25.330
So it does recognize that as a floating-point number. And it possessed all seven characters.

07:26.620 --> 07:28.330
Okay, so that's it for this video.

07:28.660 --> 07:29.610
I'll see you next time.

07:29.620 --> 07:31.780
But meanwhile, keep coding!
