본문 바로가기

Programming/C++ Basic

[Basic C++] Decimal to Binary Converter C++ Code

Here is a simple code converting decimal number to binary number.

 

Code example 1

#include <iostream>

void print_ary(int* ary, int size)
// Accept the array pointer and the length of an array
{
	for (int i = 1; i < size+1; i++)
	{
		std::cout<<(ary[size-i]);
	}
}

void dec_to_bin(int ao)
// Convert decimal value to binary value
{
	int a = ao;
	int bin_digit = log2(a)+1;
	int* bin_ary = new int[bin_digit] {0}; //Dynamic allocation on heap
	
	// Binary transformation
	for (int i = 0; i < bin_digit; i++)
	{
		bin_ary[i] = a % 2;
		a = a / 2;
	}
		
	std::cout << "Decimal number [" << ao << "]" << " is ";
	std::cout << "Binary number [";
	print_ary(bin_ary, bin_digit);
	std::cout<<"]" << std::endl;

	delete[] bin_ary; // Deallocate array value from heap
}	

int main()
{
	int exit = 1;
	while (exit == 1)
	{
		int decimal_value;

		std::cout << "Input a decimal value : ";
		std::cin >> decimal_value;
		dec_to_bin(decimal_value);
		std::cout << "Continue the program ? 1. Yes, 0. No" << std::endl;
		std::cin >> exit;
	}
	std::cin.get();
}

1. Code explanation

1.1) Function "print_ary"

The first function is to print out the elements of a given array. Unlike a python array, C++ does not print out its elements for you. Thus you need to construct a for-loop. This function accepts the reference of an array because passing an array to a function usually wastes a lot of memory. In this case, the array is very small so it's not necessary.

void print_ary(int* ary, int size)
// Accept the array pointer and the length of an array
{
	for (int i = 1; i < size+1; i++)
	{
		std::cout<<(ary[size-i]);
	}
}

1.2) Function "dec_to_bin"

The second function converts a given decimal number to a binary number. As we already learned in elementary school, a decimal number can be written as below,

 

D = a x 10^n + b x 10^(n-1) + ... + c x 10^0 , Binary system is the same,

D = a` x 2^n + b` x 2^(n-1) + ... + c` x 2^0.

 

In order to get a binary number, you can simply iterate result/remainder operations by 2 continuously until you get 1. For example, the number 10 is,

 

10 / 2 = 5 remainder is 0

5 / 2 = 2 remainder is 1

2 / 2 = 1 remainder is 0

 

Thus, 10 in decimal is 1010 in binary. Put this operation in a for-loop and save each result into an array.

In addition, you have to delete the sorted array at the end because you have dynamically allocated the array on heap.

void dec_to_bin(int ao)
// Convert decimal value to binary value
{
	int a = ao;
	int bin_digit = log2(a)+1;
	int* bin_ary = new int[bin_digit] {0}; //Dynamic allocation on heap
	
	// Binary transformation
	for (int i = 0; i < bin_digit; i++)
	{
		bin_ary[i] = a % 2;
		a = a / 2;
	}
		
	std::cout << "Decimal number [" << ao << "]" << " is ";
	std::cout << "Binary number [";
	print_ary(bin_ary, bin_digit);
	std::cout<<"]" << std::endl;

	delete[] bin_ary; // Deallocate array value from heap
}	

1.3) Main function

The main function continues to operate unless the while loop returns a false value. Each iteration returns a binary value of a given decimal value and asks whether you end the loop or not.

int main()
{
	int exit = 1;
	while (exit == 1)
	{
		int decimal_value;

		std::cout << "Input a decimal value : ";
		std::cin >> decimal_value;
		dec_to_bin(decimal_value);
		std::cout << "Continue the program ? 1. Yes, 0. No" << std::endl;
		std::cin >> exit;
	}
	std::cin.get();
}