13 Replies - 70260 Views - Last Post: 16 February 2010 - 08:18 PM Rate Topic: -----

#1 shag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-September 07

converting binary to decimal (recursion)

Post icon  Posted 19 September 2007 - 10:26 AM

i am trying to write a program that will convert a binary number into its equivalent decimal number using recursion. I believe I have everything in the code, but the math isn't right and I am hoping that is my only problem. It compiles and runs but does not give the decimal so that is why I think the math is wrong.
Any help is greatly appreciated. Here is what I have so far.....






import java.util.*;

public class BinaryConverter
{
	static Scanner scan = new Scanner(System.in);

	public static void main(String[] args)
	{
		int binaryNum;
		int base;

		base = 10;

		System.out.print("Enter a binary number: ");
		binaryNum = scan.nextInt();
		System.out.println();
		
		System.out.print("Binary " + binaryNum + " = ");
		BinToDec(binaryNum, base);
		System.out.println(" decimal");
		
	}

	public static void BinToDec(int num, int base)
	{
		if (num > 0)
		{
			BinToDec(num / base, base);
			System.out.print(num % base);
		}
   }
}
	 


Is This A Good Question/Topic? 0
  • +

Replies To: converting binary to decimal (recursion)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: converting binary to decimal (recursion)

Posted 19 September 2007 - 01:51 PM

I am not too sure if you need for it to be recursive or if this is an assignment and/or practice but you could easily write something like this below for shifting the bits over and essentially converting binary without recursion. Feel free to use the example below as you see fit and throw it into a recursive function if you really need to... even though it doesn't lend itself to such a conversion easily.

Enjoy the sample code below:

public class convert {
	public static void main(String args[]) {
		int bin[] = {1,1,0,0,1}; // Our test array containing the binary
		System.out.println(Bin2Dec(bin));
	}

	private static long Bin2Dec(int[] bin) {
		long decValue=0L;

   		for (int i=0; i<bin.length; i++) {
				  decValue <<= 1; // Shift content of decValue 1 position to the left
				  decValue += bin[i]; // add the 0 or 1
   		}

   		return decValue;	
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 shag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-September 07

Re: converting binary to decimal (recursion)

Posted 19 September 2007 - 05:41 PM

thanks for the reply and help ----- it does have to use recursion

i am trying to use the code below (and "convert" it to recursion) but am having some trouble. the code below is something i had done earlier with just a simple conversion program (that did not use recursion) -------

i still havent figured it out

 int bottom = 1;
				  int decimal = 0;
				  for (int i = Number.length () -1; i >= 0; i--) {
				  decimal = decimal + (bottom*Integer.parseInt("" + Number.charAt(i)));
				  bottom = bottom * 2; }  

Was This Post Helpful? 0
  • +
  • -

#4 kabronkline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 07

Re: converting binary to decimal (recursion)

Posted 23 September 2007 - 01:33 PM

Here is how I would implement the recursive method:

public static String decimalToBinary(int decimal, StringBuffer buffer)
{
buffer.append(decimal % 2);
decimal /= 2;
if (decimal == 0) return buffer.reverse().toString();
else return decimalToBinary(decimal, buffer);
}

The one catch is you must also pass the method a StringBuffer object so that the application can remember the previous modulus operations.

This post has been edited by kabronkline: 23 September 2007 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 joran420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 09

Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:13 PM

[edit] whoops found this page from gogle ...didnt check the post date...
how bout
function decToBin(myDecimal){
	 if(myDecimal < 2) return myDecimal;
	 return decToBin(floor(myDecimal/2))+""+myDecimal%2;
}


This post has been edited by joran420: 09 March 2009 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:24 PM

At least it doesn't use a switch :P
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:36 PM

The problem is false to start width:

System.out.print("Enter a binary number: ");
binaryNum = scan.nextInt();



If the user enters 101
nextInt() will return 101 decimal not 5 which is the translation of 101 binary

so your method receives something like "the decimal representation with 1 and 0" of a binaty number
If the user enters 100011100010110101010 you will have a decimal overflow

Your method will have to receive a String of 0 and 1 as input
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:48 PM

View PostBigAnt, on 9 Mar, 2009 - 05:24 PM, said:

At least it doesn't use a switch :P


Oh Yeah, i remember that dude from that other day..that algorithm that got the weirdest algorithm award :P
Was This Post Helpful? 0
  • +
  • -

#9 richardhawkins26  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 28-May 09

Re: converting binary to decimal (recursion)

Posted 28 May 2009 - 09:29 AM

How about this one:
#include <iostream>
#include <math.h>

using namespace std;

int binToDec(int num, int weight);

int main()
{
	int binaryNum=1001;
	int weight=0;
	int decimal;
		cout<<binToDec(binaryNum, weight)<<endl;
		return 0;
}

int binToDec(int num, int weight)
{
	
	if(num==0)
		return 0;
	else
	 	{
		 return(num%2)*pow(2,weight) +
				 binToDec(num/10, weight+1);
	   	}
}


Was This Post Helpful? 1

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: converting binary to decimal (recursion)

Posted 29 May 2009 - 10:06 PM

View Postkabronkline, on 23 Sep, 2007 - 12:33 PM, said:

Here is how I would implement the recursive method:

public static String decimalToBinary(int decimal, StringBuffer buffer)
{
buffer.append(decimal % 2);
decimal /= 2;
if (decimal == 0) return buffer.reverse().toString();
else return decimalToBinary(decimal, buffer);
}

The one catch is you must also pass the method a StringBuffer object so that the application can remember the previous modulus operations.

Cute :D :^:
Was This Post Helpful? 1

#11 Onker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: converting binary to decimal (recursion)

Posted 30 May 2009 - 07:11 AM

His problem isn't converting Decimal to Binary, it's the other way around.

The logic you need is to accept the binary number as a string.
break the string up into individual characters.
Convert to int and add them to an array.

at that point, you simply multiply the 1's and 0's according to their place in the string, as you would with binary to decimal conversion on paper.

have a variable to sum up the math (first number converted + second number converted, etc.)

Doing it recursively shouldn't be too hard, but that's the basic logic you need to make it work.
Was This Post Helpful? 0
  • +
  • -

#12 davitz38  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 19-December 09

Re: converting binary to decimal (recursion)

Posted 27 January 2010 - 02:36 AM

This is not a recursion but this tool can help to double check
binary to decimal online converter
David
Was This Post Helpful? 0
  • +
  • -

#13 Guest_vishu garg*


Reputation:

Re: converting binary to decimal (recursion)

Posted 16 February 2010 - 12:01 AM

plz give the programs using arrays of binary to decimal
Was This Post Helpful? -1

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: converting binary to decimal (recursion)

Posted 16 February 2010 - 08:18 PM

No. besides this topic is over 3 years old...We don't just give codes!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1