Binary, Octal and Hexadecimal Numbers

āļšāđˆāļ­āļĒāļ„āļĢāļąāđ‰āļ‡āļ—āļĩāđˆāļ™āļąāļāļžāļąāļ’āļ™āļēāđ‚āļ›āļĢāđāļāļĢāļĄāļ—āļēāļ‡āļ”āđ‰āļēāļ™āļĢāļ°āļšāļšāļŠāļĄāļ­āļ‡āļāļĨāļāļąāļ‡āļ•āļąāļ§ āļˆāļ°āļ•āđ‰āļ­āļ‡āđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄāđ€āļžāļ·āđˆāļ­āļĢāļąāļšāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļ•āļĢāļ‡āļāļąāļšāļ•āļąāļ§āļŪāļēāļĢāđŒāļ”āđāļ§āļĢāđŒāļŦāļĢāļ·āļ­āļĢāļ°āļšāļšāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļˆāļąāļāļĢ āļ‹āļķāđˆāļ‡āļˆāļģāđ€āļ›āđ‡āļ™āļˆāļ°āļ•āđ‰āļ­āļ‡āļĄāļĩāļāļēāļĢāļ­āđˆāļēāļ™āļ„āđˆāļēāđƒāļ™āļĢāļ°āļ”āļąāļšāļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āđ€āļĨāļ‚āļ—āļĩāđˆāđ„āļĄāđˆāđƒāļŠāđˆāđ€āļĨāļ‚āļāļēāļ™āļŠāļīāļšāļ—āļĩāđˆāļ„āļ™āļ—āļąāđˆāļ§āđ„āļ›āļ„āļļāđ‰āļ™āđ€āļ„āļĒ āđāļ•āđˆāļˆāļ°āđ€āļ›āđ‡āļ™āđ€āļĨāļ‚āļāļēāļ™āļŠāļ­āļ‡ āđ€āļĨāļ‚āļāļēāļ™āđāļ›āļ” āđāļĨāļ°āđ€āļĨāļ‚āļāļēāļ™āļŠāļīāļšāļŦāļ

āđāļĨāđ‰āļ§āļˆāļ°āļĄāļĩāđ€āļ—āļ„āļ™āļīāļ„āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āļ”āđ‰āļ§āļĒāļ āļēāļĐāļē C/C++ āđƒāļ™āļāļēāļĢāļˆāļąāļ”āļāļēāļĢāļ„āđˆāļēāļāļēāļ™āđ€āļŦāļĨāđˆāļēāļ™āļĩāđ‰āļ­āļĒāđˆāļēāļ‡āđ„āļĢ?

āđ‚āļ”āļĒāļŦāļĨāļąāļāļāļēāļĢāđƒāļ™āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄ āļ„āđˆāļēāļāļēāļ™āļŠāļīāļš (decimal value) āļŠāļēāļĄāļēāļĢāļ–āļ–āļđāļāđƒāļŠāđ‰āđ‚āļ”āļĒāđ„āļĄāđˆāļ•āđ‰āļ­āļ‡āļĄāļĩāļ•āļąāļ§āļ­āļąāļāļ‚āļĢāļ°āļāļģāļāļąāļš (prefix character) āđāļ•āđˆāļ„āđˆāļēāļāļēāļ™āļ­āļ·āđˆāļ™ āđ† āļˆāļ°āļ•āđ‰āļ­āļ‡āļĄāļĩ prefix āļ”āļąāļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡

  1. āļ„āđˆāļēāđ€āļĨāļ‚āļāļēāļ™āļŠāļ­āļ‡ (Binary value) āļˆāļ°āđƒāļŠāđ‰ "0b" (āđ€āļĨāļ‚āļĻāļđāļ™āļĒāđŒ āļ•āļēāļĄāļ”āđ‰āļ§āļĒāļ•āļąāļ§āļžāļīāļĄāļžāđŒāđ€āļĨāđ‡āļ â€˜b’) āđ€āļŠāđˆāļ™ 0b11001010

  2. āļ„āđˆāļēāđ€āļĨāļ‚āļāļēāļ™āđāļ›āļ” (Octal value) āļˆāļ°āđƒāļŠāđ‰ "0" (āđ€āļĨāļ‚āļĻāļđāļ™āļĒāđŒ) āđ€āļŠāđˆāļ™0533

  3. āļ„āđˆāļēāđ€āļĨāļ‚āļāļēāļ™āļŠāļīāļšāļŦāļ (Hexadecimal value) āļˆāļ°āđƒāļŠāđ‰ "0x" āļŦāļĢāļ·āļ­ "0X" (āđ€āļĨāļ‚āļĻāļđāļ™āļĒāđŒ āļ•āļēāļĄāļ”āđ‰āļ§āļĒāļ•āļąāļ§āļžāļīāļĄāļžāđŒāđ€āļĨāđ‡āļāļŦāļĢāļ·āļ­āļžāļīāļĄāļžāđŒāđƒāļŦāļāđˆ ‘x’ āļŦāļĢāļ·āļ­ 'X') āđ€āļŠāđˆāļ™ 0x12AB, 0x3AFF, 0Xaabf

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āļ”āđ‰āļ§āļĒāļ āļēāļĐāļē C

// base_number_1.c
// example of Binary, Octal and Hexadecimal Numbers

#include<stdio.h>

int main(void) 
{
	int value;
	
	//assigning value in Decimal format
	value = 3624;
	
	//printing value in decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in binary format
	value = 0b111000101000;
	//printing value in Decimal, octal and Hexadecimal format 
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Octal format 
	value = 07050;
	//pringing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	//assigning value in Hexadecimal format
	value = 0xe28;
	//printing value in Decimal, Octal and Hexadecimal format
	printf("Decimal: %d, octal: %o, Hexadecimal: %x\n",value,value,value);
	
	return 0;
}

āļ—āļģāļāļēāļĢ complie āđāļĨāļ°āļ”āļđāļœāļĨāļĨāļąāļžāļ˜āđŒāđ‚āļ›āļĢāđāļāļĢāļĄ

$ gcc base_number_1.c -o base_number_1
$ ./base_number_1

Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28
Decimal: 3624, octal: 7050, Hexadecimal: e28

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āļ”āđ‰āļ§āļĒāļ āļēāļĐāļē C++

// base_number_2.cpp
// example of Binary, Octal and Hexadecimal Numbers
// Credit: cppreference.com

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    cout << "The number 42 in octal:   " <<  oct << 42 << '\n'
         << "The number 42 in decimal: " <<  dec << 42 << '\n'
         << "The number 42 in hex:     " <<  hex << 42 << '\n';
    int n;
    istringstream("2A") >>  hex >> n;
    
    cout <<  dec << "Parsing \"2A\" as hex gives " << n << '\n';
   
     // the output base is sticky until changed
    cout <<  hex << "42 as hex gives " << 42
         << " and 21 as hex gives " << 21 << '\n';
}

āļ—āļģāļāļēāļĢ complie āđāļĨāļ°āļ”āļđāļœāļĨāļĨāļąāļžāļ˜āđŒāđ‚āļ›āļĢāđāļāļĢāļĄ

$ g++ base_number_2.c -o base_number_2
$ ./base_number_2

The number 42 in octal:   52
The number 42 in decimal: 42
The number 42 in hex:     2a
Parsing "2A" as hex gives 42
42 as hex gives 2a and 21 as hex gives 15

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ›āļĢāļ°āļĒāļļāļāļ•āđŒāļāļēāļĢāđāļ›āļĨāļ‡āđ€āļĨāļ‚āļāļēāļ™āļŠāļ­āļ‡ āļāļēāļ™āđāļ›āļ” āļāļēāļ™āļŠāļīāļšāļŦāļ

// base_number_3.cpp
// example of Binary, Octal and Hexadecimal Numbers
// Author: Hatim Master  

#include <iostream>
#include <iterator>
#include <algorithm>
#include <bitset>//For binary conversion


//Function with converts the number to octal number
std::string toOctal(int num) {
	std::string octalNum("");
	while(num > 0) {
		int total = num % 8;
		num /= 8;
		octalNum.append(std::to_string(total));
	}
	std::reverse(octalNum.begin(),octalNum.end());
	return (std::move(octalNum));
}

//Function with converts the number to Binary number
std::string toBinary(int num) {
	return std::move((std::bitset<8>(num).to_string()));
}

//Function with converts the number to Hexadecimal number
std::string toHex(int num) {
	std::string hexNumber("");
	while(num > 0) {
		int rem = num % 16;
		if(rem > 9){
			switch(rem) {
				case 10: hexNumber.append("A");break;
				case 11: hexNumber.append("B");break;
				case 12: hexNumber.append("C");break;
				case 13: hexNumber.append("D");break;
				case 14: hexNumber.append("E");break;
				case 15: hexNumber.append("F");break;
			}
		}
		else
			hexNumber.append(std::to_string(rem));
		num /= 16;
	}
	//Reversing the the string
	std::reverse(hexNumber.begin(),hexNumber.end());
	return(std::move(hexNumber));

}


std::string conversion(int num) {
	std::string result("\n\nDecimal: ");
	result.append(std::to_string(num));
	result.append("\n");

	//Converting to Binary
	result.append("Binary: ");
	result.append(toBinary(num));
	result.append("\n");

	//Converting to Octal
	result.append("Octal: ");
	result.append(toOctal(num));
	result.append("\n");

	//Converting to hex
	result.append("Hex: 0x");
	result.append(toHex(num));
	result.append("\n");
	return (std::move(result));
}

int main() {
	std::cout << "Enter Number: ";

	std::transform(std::istream_iterator<int>(std::cin),std::istream_iterator<int>(),
			std::ostream_iterator<std::string>(std::cout,"\nNumber:"),conversion);
}

āļ—āļģāļāļēāļĢ complie āđāļĨāļ°āļ”āļđāļœāļĨāļĨāļąāļžāļ˜āđŒāđ‚āļ›āļĢāđāļāļĢāļĄ

$ g++ base_number_3.c -o base_number_3
$ ./base_number_3

Enter Number: 45

Decimal: 45
Binary: 00101101
Octal: 55
Hex: 0x2D

Last updated

Was this helpful?