#include<stdio.h> #include<math.h> #include<string.h> int bin2dec(int decimal); int main(void) { int num; printf("The decimal equilavent is: %d", bin2dec(decimal)); getchar(); getchar(); return 0; } int bin2dec(int decimal) { int num[10]; int i; int bin; int arr1[10]; int arr2[10] = {512,256,128,64,32,16,8,4,2,1}; printf("Enter a binary(0's and 1's) number: "); scanf("%d", &arr1); This is qheere I get confused. for (i = 0; i < arr1; i++) { if (arr1[i] == 1) num[i] = arr1[i]; arr2[i]= num[i] decimal = arr2[i]; } return decimal; }

# Converting Binary to Decimal

Page 1 of 1## 12 Replies - 12264 Views - Last Post: 20 September 2010 - 09:01 PM

### #1

# Converting Binary to Decimal

Posted 24 September 2006 - 01:04 PM

##
**Replies To:** Converting Binary to Decimal

### #2

## Re: Converting Binary to Decimal

Posted 24 September 2006 - 09:46 PM

### #3

## Re: Converting Binary to Decimal

Posted 25 September 2006 - 01:02 AM

http://code.dreaminc...t/snippet30.htm

be aware that this takes binary in the form of right-to-left, meaning that the decimal value 2 is represented as 01.

whereas the left-to-right form would represent 2 as 10

### #4

## Re: Converting Binary to Decimal

Posted 25 September 2006 - 06:28 AM

int i, ch; printf("Enter an integer: "); scanf("%c",&ch); for(i=0x80; i;i=i>>1) printf("%d", ( (ch&i) ? 1 : 0 ) );

You can also use the itoa function to do this.

/* i is the number to be converted c is the buffer (char[]) where the binary number is stored. 2 represents the base which the source (ie. i ) has to be converted into. */ itoa(i,c,2); printf("%s",c);

If you can see something in the Previous code that used the For loop, you will see the possibility of using a recursive function to do this.

### #5

## Re: Converting Binary to Decimal

Posted 25 September 2006 - 07:54 AM

I am new so somethings seems difficult than they are.

This post has been edited by **Dark_Nexus**: 25 September 2006 - 08:46 AM

### #6

## Re: Converting Binary to Decimal

Posted 25 September 2006 - 05:05 PM

#include "stdafx.h" #include <stdio.h> #include<math.h> int bin2dec(int decimal); int main() { int decimal; printf("Enter an integer: "); scanf_s("%c",&decimal); printf("The decimal equivalent = %d\n",bin2dec(decimal)); getchar(); getchar(); return 0; } int bin2dec(int decimal) { int total = 0; int power = 1; while(decimal > 0) { total += decimal % 10 *power; decimal = decimal / 10; power = power * 2; } return decimal; }

This post has been edited by **browngod2002**: 25 September 2006 - 05:15 PM

### #7

## Re: Converting Binary to Decimal

Posted 25 September 2006 - 05:59 PM

scanf_s("%d",&decimal);

It would be of great benefit if you describe in what way your application is not working, so that someone can better help you.

List error messages, incorrect output, compilation errors.

### #8

## Re: Converting Binary to Decimal

Posted 26 September 2006 - 03:05 AM

snippet...... switch(choise) { case 1: cout << "Enter a Hex number: "; cin >> hex >> number; cout << "\n value in octal = " << oct << number << endl; cout << " value in decimal = " << dec << number << endl; break; case 2: cout << "Enter a Octal number: "; cin >> oct >> number; cout << "\n value in hex = " << hex << number << endl; cout << " value in decimal = " << dec << number << endl; break; case 3: cout << "Enter a dec number: "; cin >> dec >> number; cout << "\n value in octal = " << oct << number << endl; cout << " value in hex = " << hex << number << endl; break; case 4: cout << "Program terminated by user..."; exit(0); default : cout << "ERROR ~ Invalid selection\n\n"; break; }

If there arn't any objections I can put the compelted code up if you wish.

This post has been edited by **Vagabond**: 26 September 2006 - 03:17 AM

### #9

## Re: Converting Binary to Decimal

Posted 26 September 2006 - 11:49 AM

#include "stdafx.h" #include <stdio.h> #include <math.h> /* function prototype */ int bintodec(int decimal); int _tmain(int argc, _TCHAR* argv[]) { int decimal; printf("Enter an interger (0's and 1's): "); scanf_s("%d", &decimal); printf("The decimal equilavent is %d.\n", bintodec(decimal)); getchar(); getchar(); return 0; } int bintodec(int decimal) { int total = 0; int power = 1; int number = 0; decimal--; while(decimal >=0) { /* this is where the problem is I need some advice on how to fix it.*/ total += decimal % 10 * power; decimal = decimal / 10; power = power * 2; number += decimal; decimal--; } return number; }

This post has been edited by **Dark_Nexus**: 26 September 2006 - 09:54 PM

### #10

## Re: Converting Binary to Decimal

Posted 26 September 2006 - 11:54 AM

### #11

## Re: Converting Binary to Decimal

Posted 26 September 2006 - 03:50 PM

#include "stdafx.h" #include <stdio.h> #include <math.h> /* function prototype */ int bintodec(int decimal); int _tmain(int argc, _TCHAR* argv[]) { int decimal; printf("Enter an interger (0's and 1's): "); scanf_s("%d", &decimal); printf("The decimal equilavent is %d.\n", bintodec(decimal)); getchar(); getchar(); return 0; } int bintodec(int decimal) { int total = 0; int power = 1; while(decimal > 0) { total += decimal % 10 * power; decimal = decimal / 10; power = power * 2; } return total; }

This post has been edited by **browngod2002**: 26 September 2006 - 03:52 PM

### #12

## Re: Converting Binary to Decimal

Posted 20 September 2010 - 08:54 PM

### #13

## Re: Converting Binary to Decimal

Posted 20 September 2010 - 09:01 PM

#include <iostream> #include <math.h> using namespace std; int bin, decimal = 12, decimal2; int base_one = 10, base_two = 2; int convert(int num, int starting_base, int destination_base); int main (int argc, char * const argv[]) { bin = convert(decimal, base_one, base_two); cout << "Base"<< base_one<< " "<< decimal << " = Base"<< base_two<< " " << bin << endl; decimal2 = convert(bin, base_two, base_one); cout << "Base"<< base_two << " " << bin << " = Base"<< base_one << " " << decimal2 << endl; return 0; } int convert(int num, int starting_base, int destination_base) { int val = 0, count = 0; while (num) { val += pow(starting_base, count) * (num % destination_base); count++; num /= destination_base; } return val; }