4 Replies - 1007 Views - Last Post: 12 October 2011 - 08:53 PM Rate Topic: -----

#1 kurobara14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-January 11

convert binary number to decimal;

Posted 11 October 2011 - 11:53 PM

i want to change binary number to decimal, below is my code.
#include <stdio.h>
int main ()
{
	int n, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16, a[16];
	do{ 
		printf("please input 16bit of binary number with space\n"); // input number//
		scanf("%d",&a[0]);
		scanf("%d",&a[1]);
		scanf("%d",&a[2]);
		scanf("%d",&a[3]);
		scanf("%d",&a[4]);
		scanf("%d",&a[5]);
		scanf("%d",&a[6]);
		scanf("%d",&a[7]);
		scanf("%d",&a[8]);
		scanf("%d",&a[9]);
		scanf("%d",&a[10]);
		scanf("%d",&a[11]);
		scanf("%d",&a[12]);
		scanf("%d",&a[13]);
		scanf("%d",&a[14]);
		scanf("%d",&a[15]); // read all the number into their boxes//
		printf(" ->\n"); // it will be (input number) -> (output number)//
		n1 = a[0]*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2;
		n2 = a[1]*2*2*2*2*2*2*2*2*2*2*2*2*2*2;
		n3 = a[2]*2*2*2*2*2*2*2*2*2*2*2*2*2;
		n4 = a[3]*2*2*2*2*2*2*2*2*2*2*2*2;
		n5 = a[4]*2*2*2*2*2*2*2*2*2*2*2;
		n6 = a[5]*2*2*2*2*2*2*2*2*2*2;
		n7 = a[6]*2*2*2*2*2*2*2*2*2;
		n8 = a[7]*2*2*2*2*2*2*2*2;
		n9 = a[8]*2*2*2*2*2*2*2;
		n10 = a[9]*2*2*2*2*2*2;
		n11 = a[10]*2*2*2*2*2;
		n12 = a[11]*2*2*2*2;
		n13 = a[12]*2*2*2;
		n14 = a[13]*2*2;
		n15 = a[14]*2;
		n16 = a[15]*1; // multiply all the binary number with 2//
		n= n1 +n2+n3+n4+ n5+ n6+ n7+ n8+ n9+ n10+ n11+ n12+ n13+ n14+ n15+ n16;
		printf("%d\n\n\n",n);
		}
	while(1);
}



my problems are
1. i only can use this program for 16 bit of binary number (0000 0000 0000 0000). how if i want to make until 64bit or more??
2. this is too troublesome to repeat same things..can anyone help me to simple it out??

i appreciate any suggestions and idea

:bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: convert binary number to decimal;

#2 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Re: convert binary number to decimal;

Posted 12 October 2011 - 12:29 AM

wont it be easier to read in the binary as a char array and work on each character, converting to integer and using its index? in this case, the bit size (16 or 64) is achieved by forcing char array length. you could then ignore spaces or any seperator you choose to use! should be something like this in the snippets
Was This Post Helpful? 1
  • +
  • -

#3 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: convert binary number to decimal;

Posted 12 October 2011 - 12:31 AM

Loops are definitely going to be your best friend. Perhaps this tutorial will help you grasp them.
Was This Post Helpful? 1
  • +
  • -

#4 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: convert binary number to decimal;

Posted 12 October 2011 - 12:34 AM

Have you heard of the bitshift operator? It basically shifts everything by the number of bits given, and takes the form - number << number of bits / number >> number of bits. To demostrate 0001 << 1 = 0010. There's other bitwise operations you can do, like &, | and ^.

As a little sidetrack, the bitshift is a really fast way of multiplying by 2. Anyway, with bitshift you can do something like this:
int val= 0;
int size = sizeof(int)*8;

for(i = 0; i < size; i++)
{
    val << 1;    
    val += a[i];
}



That way, you can also change the type, and it will still work, but you need to be careful the array given is the right size.

This post has been edited by nuclearfroggy: 12 October 2011 - 01:09 AM

Was This Post Helpful? 1
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: convert binary number to decimal;

Posted 12 October 2011 - 08:53 PM

The raise() function operates on a binary string bin. Don't think it is fussy about number of bits.

 cout<<"Enter a binary number\n";
    getline(cin,bin);
    cout<<"\n";
    int n=2,p=bin.length();
    result=raise(n,p);
    for(int i=0;i<p;i++)
      {   if(bin[i]=='1')sum+=result;
          result/=2;}
    cout<<"\n";
    cout<<bin<<" is equivalent to "<<sum-1<<" in Hindu/Arabic"<<endl;

double raise(int n,int p)
    {double result=1;
       for(int i=0;i<p-1;i++)
         result*=n;//result = n * n by p times
         return result;
    }

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1