
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Octal to Binary Converter in C++
In a computer system, the binary number is expressed in the binary numeral system while the octal number is in the octal numeral system. The binary number is in base 2 while the octal number is in base 8.
Here, we provide the tabular data to understand binary numbers and their corresponding octal numbers as follows:
Octal Number | Binary Number |
---|---|
15 | 01101 |
5 | 00101 |
26 | 10110 |
12 | 01010 |
Octal to Binary Conversion Using stoi() and bitset
In C++, both stoi() and bitset are used to work with numeric conversion and binary representation.
- stoi: This is used for string number conversion.
- bitset: This is used for bit manipulation.
Syntax
Its syntax is as follows:
int num = stoi(octal_str, nullptr, 8); string bin = bitset<16>(num).to_string();
Example
In this example, we converts the octal value "17" to decimal (15) and then displays it as an 8-bit binary number.
#include <iostream> #include <bitset> #include <string> using namespace std; int main() { string octal = "17"; int num = stoi(octal, nullptr, 8); string binary = bitset<8>(num).to_string(); cout << "The Binary result is " << binary; return 0; }
Output
The above program obtained the following result:
The Binary result is 00011111
Octal to Binary Conversion Using Digit-wise Mapping
In this approach, each octal digit is individually mapped to a 3-digit binary equivalent using a switch-case structure.
Example
Here, each character of octal string is matched and replaced with 3-bit binary string.
#include <iostream> #include <string> using namespace std; int main() { string octal = "25"; string binary = ""; for (char digit : octal) { switch (digit) { case '0': binary += "000"; break; case '1': binary += "001"; break; case '2': binary += "010"; break; case '3': binary += "011"; break; case '4': binary += "100"; break; case '5': binary += "101"; break; case '6': binary += "110"; break; case '7': binary += "111"; break; } } cout << "The Binary result is " << binary; return 0; }
Output
The above code obtained the following output:
The Binary result is 010101
Octal to Binary Conversion Using Lookup Table
This approach explain about mapping using a lookup array that convert each octal digit into binary form.
Example
This example shows the usage of lookup table under the for loop.
#include <iostream> #include <string> using namespace std; int main() { string octal = "73"; string lookup[] = {"000", "001", "010", "011", "100", "101", "110", "111"}; string binary = ""; for (char digit : octal) { binary += lookup[digit - '0']; } cout << "The Binary result is " << binary; return 0; }
Output
The above code produces the following output:
The Binary result is 111011