Site icon Learn C++

How To Use Base-8 (Octal) Numbers In Modern C++?

How To Use Base 8 Octal Numbers In Modern C++

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++.

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,

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.

[crayon-662d1f1265aa6385068915/]

How to initialize base-8 (octal) number in modern C++?

[crayon-662d1f1265aae540784020/]

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,

[crayon-662d1f1265ab0840613098/]

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,

[crayon-662d1f1265ab2994981927/]

How to print out base-8 (octal) number in C language?

C has very powerful printf() function and there is a ‘%oFormat Specifier to print out octal numbers.

[crayon-662d1f1265ab9516200645/]

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 ‘%oFormat Specifier to print out octal numbers that can be used as below,

[crayon-662d1f1265abb084063008/]

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:

[crayon-662d1f1265abd246607856/]

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,

[crayon-662d1f1265abf972687844/]

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:

[crayon-662d1f1265ac3779629669/]

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.

Exit mobile version