# converting binary to decimal (recursion)

Page 1 of 1

## 13 Replies - 88759 Views - Last Post: 16 February 2010 - 08:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=33592&amp;s=f595d006059d0cdc1fd0d609b3e29510&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shag

• New D.I.C Head

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

# converting binary to decimal (recursion)

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

• Programming Theoretician

Reputation: 4925
• Posts: 13,317
• 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;
}
}

```

### #3 shag

• New D.I.C Head

Reputation: 0
• 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; }
```

### #4 kabronkline

• New D.I.C Head

Reputation: 0
• 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

### #5 joran420

• New D.I.C Head

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

## Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:13 PM

 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

### #6 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• 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

### #7 pbl

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

Reputation: 8370
• Posts: 31,956
• 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

### #8 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: converting binary to decimal (recursion)

Posted 09 March 2009 - 06:48 PM

BigAnt, on 9 Mar, 2009 - 05:24 PM, said:

At least it doesn't use a switch

Oh Yeah, i remember that dude from that other day..that algorithm that got the weirdest algorithm award

### #9 richardhawkins26

• New D.I.C Head

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

## Re: converting binary to decimal (recursion)

Posted 28 May 2009 - 09:29 AM

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

```

### #10 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: converting binary to decimal (recursion)

Posted 29 May 2009 - 10:06 PM

kabronkline, 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

### #11 Onker

Reputation: 3
• 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.

### #12 davitz38

• New D.I.C Head

Reputation: -1
• 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

Reputation:

## Re: converting binary to decimal (recursion)

Posted 16 February 2010 - 12:01 AM

plz give the programs using arrays of binary to decimal

### #14 Dogstopper

Reputation: 2956
• Posts: 11,220
• 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!