In this video, we'll see some of the stream formatting options available for integers.
Integers have 4 basic formatting options.
Let's see what the defaults are for those options.
First, integers are displayed in base 10 by default.
This makes sense since base 10 is what most people in the world use as their base system.
However, we also have the option of displaying integers in base 8,
which is called octal and base 16 which is called hexadecimal.
Remember, computers work in base 2 or binary.
And that's difficult for humans to understand.
So we need alternate base systems. Base 10 is what we learned in school,
Base 10 is what we learned in school, and that's the way we count and do math.
But it's not so intuitive to convert decimal to binary.
The alternate bases are often used in computing
since they allow us to group information more like a computer would.
For example, since octal is base 8,
we can group consecutive binary digits into groups of 3
starting from the right,
and we can easily convert between octal and binary and back again.
Hexadecimal is base 16, which allows us to represent locations
in memory since each hexadecimal digit represents 8 bits or 1 byte.
If you recall, earlier in the course, when we displayed addresses,
they were displayed in hexadecimal format.
We also have the option to display the base as a prefix for hex
and octal values.
For hex, we start with 0x followed by the hex number.
And for octal, we start with a 0.
This clearly tells the reader that the number is hex or octal since in some cases
it would be impossible to tell otherwise.
The default is not to show the base.
We can show the letters in hex numbers as well as the hex prefix in
upper or lower case and lowercase is the default.
And finally, we can choose to display the plus sign when we have positive integers.
The default, as we've seen already, is not to display the plus sign when an integer is positive.
This might sound complicated and intimidating, but it's easy as pi.
Let me show you how we can use the iostream manipulators
to change the way we format integers.
First, let's see an example of displaying integers using different bases.
Here we initialize num to 255. That is 255
in base 10, just like we're used to doing.
Now we can use the base manipulators to change the way the integer is formatted.
Remember, even though the output changes,
255 is still stored in num, and we're not going to change it.
First, we use stood des and that means decimal,
and that displays the number in base 10.
We really don't have to specify this at all since the default,
but I wanted to show you how to do it, and this will display 255.
The second display statement uses std hex.
In this case, we display ff, which is the hexadecimal value for 255.
Also note that the f's are in lower case which is also the default.
Finally, we have the oct manipulator,
and we display 277, which is the octal value
for decimal 255.
Once we set the base using the manipulator,
that base will be used for all future insertions of any integer on the output stream,
so that setting will persist.
Now if we see 255 and 277 out of context,
it would be virtually impossible to tell whether they were base 8 or base 10,
so that's where the show base manipulator comes in handy, I'll show you that in the next slide.
So in this example, we have the same code except we're using the show base manipulator.
Now when we execute the same code,
notice that 255 is unchanged, but the hexadecimal ff
now has a 0x prefix, and it's in lower case.
Also, the octal 377 now has a 0 preceding it.
Now it's very clear what the base of each of these numbers is.
Also note that the showbase is applied
to all further integer stream insertions, so it also persists.
We can toggle back to not showing the base by using std no show base.
Sometimes we want to be able to display hexadecimal digits in upper case as well.
And sometimes we wan to display the 0x prefix in uppercase.
We can do this with the uppercase manipulator.
In this case, we're using showbase and uppercase.
And then we use the hex manipulator and display the number.
Notice how we display 0xff and all the letters are uppercase. Like showbase,
uppercase will be used for all further hex output,
and we can toggle back to lowercase using no uppercase.
Now let's see how the show positive manipulator works.
If we use showpos or show positive,
then we'll get a preceding plus sign on positive integers.
You can see in the provided code that we have 2 integers,
num1 which is initialized to 255, and num2
which is initialized to minus 255.
Without using the showpos manipulator,
if we display these integers, we get 255 and negative 255.
Note that there's no plus sign in front of the positive 255.
However, when we use the showpos manipulator,
now we see the plus sign in front of the positive 255.
This can be useful sometimes depending on the application,
some accounting software wants that plus sign in front of the number.
Again, this setting also persists, so it'll apply to any other integer
stream output.
And again, we can toggle back
by to not showing the plus sign with the no showpos manipulator.
That's it. I told you it was easy.
There are also several variations using the setf method,
and we can also reset to defaults using std reset iOS flags
and providing the flags we want. I encourage you to do as I do,
that is not memorize any of these things but just know where to look them up.
The c++ reference documentation is pretty good, and everything's there.
Unless, of course, you want to memorize them, that's up to you.
Okay. So let's head over to the IDE and try some of these combinations out.
Okay. So I'm in the IDE. I'm in the section 19 workspace
in the manip_integer project.
And what I'm doing in this project just manipulating integers,
just like we did in the slides. I'm converting them
to different bases. Then I'm playing around with some of these manipulators.
So let's start by, first of all, including iostream and
iomanip. We need to do that too in order to use our manipulators.
And I've got an integer here that I've defined and initialized
to 255, it's called num.
Okay. So let's start right here. Here what we're doing is we're displaying num.
And again, it's only the 1 variable. We're going to use throughout this example.
We're displaying it in decimal format. Again, we don't really need to do this here since it's the default
and then in hex and then an octal and. So when we run this,
we see 255 ff, which is the hex and 377,
which is octal, just like we saw in the slides. So that's pretty straightforward.
And now, like I said, sometimes if you see those
2 integers or those 2 series of numbers,
you really don't know what the base is.
So we can use the showbase manipulator right here,
as I'm using right here on line 18.
And now when I run it, we'll see the base.
So now we're going to see the 0x right here in front of the hexadecimal number
and the 0 in front of the octal number, which is
pretty nice that way you know exactly what the context, right.
So you know this is hex and you know that this is octal.
And obviously, you know that just as a decimal up here.
Okay. I�ll tell you a funny story. A couple years ago, I was interviewing a
candidate for a c++ programming position,
and it was an entry level position.
You ask them to write code and to, ask them some questions about the language.
So I asked them to write a program that accepted an integer and converted it to a hexadecimal number.
And every other candidate that I've ever
asked that kind of question to goes off and writes a function and does all kinds of stuff.
This kid went over wrote the program in like
30 seconds and told me he was done,and I couldn't believe it.
So when I went over there and just ran it,
his output was correct.
And what he did was this, which was pretty clever, right.
All he did was ask me for the integer,
did a std hex and printed it out. I was very, very clever,
a very bright kid.
I never would have thought about doing that in an interview, but anyway he got the job,
and he did really well in the end. So that's a good way to think outside the box.
Sometimes you don't want to go for the fancy algorithms, just do it the easy way.
I'm all for that, but anyway.
So here now what we're going to do is,
notice that let me run this again so that you can see that
the 0x and the ff here and the hex number are lower case.
If you need to have them in uppercase,
we can use std uppercase, just like we are here on line 25.
And when I run that now, you'll see that,
let me put that comment back.
So when i run this now, you can see that now it's a 0xff, and those are capitalized.
That only applies to hexadecimal numbers because the hex numbers
are the only ones that have letters in them.
Okay. The showpos or show positive modifier right here.
So let me uncomment this code out.
And what this does, it only works with decimal values.
And if the number is positive, then it displays the plus sign in front of the number.
By default, we don't display a plus sign in front of the number.
So let's run this. And you can see that right here on the 255,
you can see that plus sign right in front of it.
And that's exactly what that does. Notice that it has no effect on hex or octal,
it only works on decimals.
And in this case, there is a method called setf.
And we can see -- and this isn't a manipulator. This is a method call.
So we're saying std cout.setf. So setf is a method.
And then we can pass in these format flags.
And what this will do is it'll set it to showbase or no showbase
or uppercase or no uppercase, whatever you really want here.
So it's an alternate way of doing what we did up here.
Okay. And you tend to see this style more often than this style.
But you'll see this sometimes out there I just wanted to show you.
Okay. Also we can use
std reset iOS flags to set these back to normal.
So for example, this one right here std iOS base field,
that'll set your integer base back to decimal. Okay.
Showbase will set it back to the default for showbase, which is
not to show the base, right. That's the default. Again, remember, this resets it back to the defaults.
This one show positive says it sets it back to not showing the positive.
And the upper case, it goes back to not showing uppercase.
Again, so don't let that confuse you. It's not we're not setting it to those flags.
We're just telling it to set it to the default.
Okay. So in this last example, I'm just using a real simple
input statement
Let me uncomment that code out.
And I'm asking the user for an integer. And I'm putting it right back into that
num variable that we had before. And I'm just using default
to display it as a decimal.
And then as a hex, and then hex upper case,
and then hex again. And the point here is so that you can see that these settings stick,
so this upper case will still be uppercase even though we've already used it
because they are persistent.
And then no uppercase
and. Then finally, octal hex, we're going to show the base and octal at the end.
So again, really, really straightforward. Let me run it. And
we can use a number like, let's say, 135.
And there you go. An integer is 135, the decimal value is 135.
In hex, it's 87.
And you can see the showbase here
because I've got
no showbase, right, because that was my default. Right here
I set it back to the default to not showbase, and that's why we're not seeing it.
Notice that when we're not showing the base, this uppercase
has no effect at all, which is exactly what you would expect, right.
So that's it. In the next video,
what we're going to do is we're going to look at the manipulators for floating point numbers and see how they work.