Converting Binary to Decimal

Page 1 of 1

12 Replies - 11695 Views - Last Post: 20 September 2010 - 09:01 PM Rate Topic: -----

#1 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Converting Binary to Decimal

Posted 24 September 2006 - 01:04 PM

I am trying to convert a binary number input by a user into a decimal number. I know how to so it on paper but getting C to do it I am lost. I am using Dev-C compiler. Here is what I got so far.

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Converting Binary to Decimal

#2 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Converting Binary to Decimal

Posted 24 September 2006 - 09:46 PM

Ok, I'm not even going to pretend to know how to answer your question. I know a little C++ but I think your beyond me here. But if I were you I might view a web page like this which does the same thing with javascript and maybe look at how some of the functions they used worked. I know don't know if this will help you but good luck.
Was This Post Helpful? 0
  • +
  • -

#3 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Converting Binary to Decimal

Posted 25 September 2006 - 01:02 AM

the following snippet contains functions which convert alpha-numeric values (which are decimal representations of letters) into binary. and towards the bottom of that snippet post, there are functions which convert binary back into the decimal values.

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
Was This Post Helpful? 1
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Converting Binary to Decimal

Posted 25 September 2006 - 06:28 AM

Always use the Bitwise AND Operator for such conversions.
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.
Was This Post Helpful? 0
  • +
  • -

#5 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Re: Converting Binary to Decimal

Posted 25 September 2006 - 07:54 AM

Thank you born2c0de that was helpful
I am new so somethings seems difficult than they are.

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

Was This Post Helpful? 0
  • +
  • -

#6 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Re: Converting Binary to Decimal

Posted 25 September 2006 - 05:05 PM

I am trying to convert bin to dec without putting it into an array or a string here is the code I have so far. I need someone to tell me why it is not working. I am using VC++ 2005 Express Edition.

#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

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Converting Binary to Decimal

Posted 25 September 2006 - 05:59 PM

I believe the problem lies here scanf_s("%c",&decimal); (although I did not put this in a compiler to check it). Your variable decimal is of type integer, but you are using the character format specifier. Change your format specifer to match your variable type.

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.
Was This Post Helpful? 0
  • +
  • -

#8 Vagabond  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 06

Re: Converting Binary to Decimal

Posted 26 September 2006 - 03:05 AM

This is a small program I wrote some time ago I don't know if the concepts will help.
 
 
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

Was This Post Helpful? 0
  • +
  • -

#9 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Re: Converting Binary to Decimal

Posted 26 September 2006 - 11:49 AM

When I build and run the program I get no errors or warnings but the output is all wrong. I think it is in my formula but I can't get the formula to work right.Here is the updated code.

#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

Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Converting Binary to Decimal

Posted 26 September 2006 - 11:54 AM

Maybe the following link will help explain how to convert.

http://www.is.wayne....inary/page3.htm
Was This Post Helpful? 0
  • +
  • -

#11 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Re: Converting Binary to Decimal

Posted 26 September 2006 - 03:50 PM

Thank all of you that helped me figure this problem out. I was returning the wrong value in my function(returning decimal instead of total). Here is my finished code and I have also posted it in the code snippets page for future user that may have the same problem that i did.

#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

Was This Post Helpful? 2

#12 boelegg52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 10

Re: Converting Binary to Decimal

Posted 20 September 2010 - 08:54 PM

Thanks everybody for the information on converting binary to decimal
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,582
  • Joined: 08-August 08

Re: Converting Binary to Decimal

Posted 20 September 2010 - 09:01 PM

Belated, but here's a more generic method I wrote.

#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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1