CC++C++11C++14C++17Learn 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,

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

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

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,

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,

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.

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,

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:

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,

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:

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?