In general, in nearly all programming languages, including Modern C++, we use base-10 numbers, while our computer hardware uses the binary base-2 system (bits). I personally believe that base-10 is because we typically have 10 fingers and since we learn to counting on our fingers we use base-10 primarily. However, due to the way binary works it is sometimes convenient to use base-8 system octal (oct) and base-16 system hexadecimal (hex) numbers in programming. In this post, we explain how to use Base-8 octal numbers in Modern C++.
Table of Contents
What is the octal number system?
The octal – or oct – number system is the base-8 system, Octal uses the digits from 0 to 7. In general, we use a base-10 number system in normal life. In the digital world we also use base-2, base-8, and base-16 as well as base-10. If you want to explore the different numbering systems, try this Wikipedia article.
Let’s give some octal examples and their values in the decimal system,
- 1octal = 1
- 10octal = 8
- 100octal = 64
How to declare base-8 (octal) number in modern C++?
In C++, when we use octal numbers in C++ code, we put 0
on the left side of an octal number. This zero shows it is an octal representation.
1 2 3 4 5 6 |
int x; // 015 in octal is 13 base10 or decimal x = 015; |
How to initialize base-8 (octal) number in modern C++?
1 2 3 |
int x = 0010; |
here the first zero on the left side of 0010
defines this number representation is an octal representation. The integer x value is results as 8;
How to set an integer to base-8 (octal) number in modern C++?
We can use std::istringstream()
which is declared in <sstream>
library. Here Octal number “010
” is declared into x as a octal number, now when you print this variable, it will be displayed in octal decimal system,
1 2 3 4 5 |
int x; std::istringstream("010") >> std::oct >> x; std::cout << x << std::endl; |
How to print out base-8 (octal) number in modern C++?
When we print out base-8 (octal) numbers we use std::oct
format as below,
1 2 3 4 |
int x = 0010; std::cout << std::oct << x << std::endl; |
How to print out base-8 (octal) number in C language?
C has very powerful printf()
function and there is a ‘%o
‘ Format Specifier to print out octal numbers.
1 2 3 |
printf("%o \n", x); |
How to display base-8 (octal) number in C++ Builder?
C++ Builder is using UnicodeString in most of components, and UnicodeString very powerful printf()
method. There is a ‘%o
‘ Format Specifier to print out octal numbers that can be used as below,
1 2 3 4 5 6 |
int i = 64; // decimal UnicodeString ustr; ustr.printf("%o \n", i); Edit1->Text = ustr; |
How to convert base-8 (octal) number to (base-10) decimal number in modern C++?
When you declare an integer with octal representation, it is automatically converted to base-10 number. For example:
1 2 3 |
int a = 0010; |
here a = 8
in the decimal system.
there is another method std::stoi
that converts octal representation in string to an integer. Here is an example,
1 2 3 4 |
int x = std::stoi("011", 0, 8); std::cout << x << std::endl; |
How to convert (base-10) decimal number to base-8 (octal) number in modern C++?
In C++, computation is done in base-2 while you have a decimal number. If you want to convert base-10 decimal number to base-8 octal number, you only need to convert to a octal number or string when you want to display. Here is how you can do:
1 2 3 4 |
int x = 64; std::cout << std::oct << x << std::endl; |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.