Saturday 26 November 2016

Very Very large decimal numbers to binary.

Hi all,
I said in my previous post that I'd be working on the problem of the range of input to the program for converting decimal numbers to binary numbers.
The largest number that the program could convert was the maximum number in the range of int. 
But, I worked on a solution that used a string as an input.

So basically, what the program does is as follows:
1. Take the input from the user as a character array named num_tmp (which is made using dynamic memory allocation as it is temporary).

Example :       "12"           //num_tmp

2. Then we have two more arrays namely num and bin. The former stores the actual input number in the form of separate digits. The latter stores the boutput binary number in the same manner but in reverse order(as we read the output binary number upside down while converting a decimal number to binary number).

But there is a problem in storing numbers as their digits - how to you know where the last digit is?
The solution is rather simple. I just added 100 to the integer array after all the digits were converted from string num_tmp (which is terminated by '\0' that helps identify the last digit.) and stored in num. Now the logic is that a single digit lies in the interval [0,9].
For the input in above example, 
                        {1, 2, 100,...}      //num
3. Then we start a loop with the condition that num is not equal to zero.

Now, how can we check if num is zero or not?
I defined a separate function for the purpose. What it does is check every digit of the array and if all the digits (before 100) are zeroes then the number is zero otherwise not.
Inside the loop we use primary school arithmetic to divide the number by 2 and simultaneously adding the remainders (0 or 1 - the binary digits) to the array called bin meant to store the result. A variable j that stores number of digits in bin is incremented and there is no need to terminate bin with 100.

*The remainders are calculated by modding the last digit with 2 (food for thought).

The arrays after the iterations of the loop are shown:

num : {1, 2, 100,...} is_zero(num) : 0    bin : {...}      j : 0
num : {0, 6, 100,...} is_zero(num) : 0   bin : {0,...}   j : 1
num : {0, 3, 100,...} is_zero(num) : 0   bin : {0, 0,...} j : 2
num : {0, 1, 100,...} is_zero(num) : 0   bin : {0, 0, 1,...} j :3
num : {0, 0, 100,...} is_zero(num) : 1   bin : {0, 0, 1, 1,...} j :4

The final result is 1100

Thats it!

Download the code here.

Here is the program in action:



Unfortunately the online decimal to binary converter supported upto 63 digit binary numbers so I can't verify the results over that. Converting this large binary numbers to decimal numbers is another problem.
I'll try to solve this problem in the next post.

However lets see how this program works with extremely large numbers:

  
 As you can see the program works with extremely large numbers. I'll try to make a program to be able to convert numbers this large back to decimal.

See you later. 
Happy coding!

P.S. C Seekers' Facebook page coming soon. Don't forget to like it.
 

No comments:

Post a Comment