# converting binary to decimal

Page 1 of 1

## 10 Replies - 7043 Views - Last Post: 19 August 2008 - 02:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=59885&amp;s=ff90e9629b17017d26c0ada442f67533&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 patjoy_24

Reputation: 0
• Posts: 1
• Joined: 06-August 08

# converting binary to decimal

Posted 06 August 2008 - 11:01 PM

please help me in my requirement in janva subject OOPin how to convert decimal to binary. plez
Is This A Good Question/Topic? 0

## Replies To: converting binary to decimal

### #2 1lacca

• code.rascal

Reputation: 44
• Posts: 3,822
• Joined: 11-August 05

## Re: converting binary to decimal

Posted 06 August 2008 - 11:29 PM

Please describe how would you do it yourself without a computer. Once you have it written down, we'll help you "translate it into Java".

### #3 lordms12

• D.I.C Regular

Reputation: 30
• Posts: 339
• Joined: 16-February 08

## Re: converting binary to decimal

Posted 07 August 2008 - 07:45 AM

Title "converting binary to decimal"
```String binaryString = "1010";
System.out.println(Integer.parseInt(binaryString, 2));
```

Post "convert decimal to binary"
```int i = 10;
String binaryString = Integer.toBinaryString(i);
System.out.println(binaryString);
```

This code is not what you want but I guess but I guess it would help

This post has been edited by lordms12: 07 August 2008 - 07:47 AM

### #4 JeroenFM

Reputation: 18
• Posts: 195
• Joined: 30-June 08

## Re: converting binary to decimal

Posted 07 August 2008 - 01:13 PM

Didn't I make a post a while ago that explained the theoretical basics of binary to decimal conversion?

Anyway:

Each bit in a binary number is a power of 2, starting with 2^0. So the nTH bit represents the value of 2^n, where n>=0 and we count n from right to left.

Therefore:

0101 = 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 0 + 4 + 0 + 1 = 5

Now surely you can turn this into an algorithm?

Reputation: 0
• Posts: 2
• Joined: 14-October 07

## Re: converting binary to decimal

Posted 13 August 2008 - 07:57 AM

nice solutions

This post has been edited by eradcoil: 13 August 2008 - 08:02 AM

### #6 dobbersp

Reputation: 2
• Posts: 23
• Joined: 10-June 08

## Re: converting binary to decimal

Posted 13 August 2008 - 11:15 AM

I think this may be a non-code question.

If so, then I hope this will prove helpful...its cool either way:

in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.

10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010

### #7 JeroenFM

Reputation: 18
• Posts: 195
• Joined: 30-June 08

## Re: converting binary to decimal

Posted 18 August 2008 - 05:45 AM

dobbersp, on 13 Aug, 2008 - 11:15 AM, said:

I think this may be a non-code question.

If so, then I hope this will prove helpful...its cool either way:

in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.

10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010

42 / 2 = 21, remainder = 0
21 / 2 = 10, remainder = 1
10 / 2 = 5, remainder = 0
5 / 2 = 2, remainder = 1
2/2 = 1, remainder = 0
1/2 = 0, remainder = 1

42 = 101010

Well what do you know, it works :-)

Let's see if I can do the same for octal:

42 / 8 = 5, remainder = 2
5 / 8 = 0, remainder = 5

42 = 52

42 / 16 = 2, remainder = 10 = A
2 / 16 = 0, remainder = 2

42 = 2A

---

However, the original post was about converting binary to decimal, not the other way around (still good to know though).

I also wrote a tutorial about it:

http://www.dreaminco...wtopic59949.htm

This post has been edited by JeroenFM: 18 August 2008 - 05:48 AM

### #8 Ak-Emm

Reputation: 1
• Posts: 22
• Joined: 18-August 08

## Re: converting binary to decimal

Posted 18 August 2008 - 07:56 AM

patjoy_24, on 6 Aug, 2008 - 11:01 PM, said:

please help me in my requirement in janva subject OOPin how to convert decimal to binary. plez

Do you mean mathematically as in the manual process of pen and paper?

or for java iprogramming itself

sites
www.java-tutorials.com
www. java-coffebreak.com something like that just search thru good sites that give programing tips

### #9 Ak-Emm

Reputation: 1
• Posts: 22
• Joined: 18-August 08

## Re: converting binary to decimal

Posted 18 August 2008 - 08:03 AM

patjoy_24, on 6 Aug, 2008 - 11:01 PM, said:

please help me in my requirement in janva subject OOPin how to convert decimal to binary. plez

manually guys here have given you a great way of solving it

just divide by base 2 for Dec-Bin

for Bin-Dec ^2 i.e 0011 ----------> 1*2^0 + 1*2^1 + 0*2^2 etc

or the alternative is use the conversion table which is the best short cut once mastered

### #10 Ak-Emm

Reputation: 1
• Posts: 22
• Joined: 18-August 08

## Re: converting binary to decimal

Posted 18 August 2008 - 08:08 AM

dobbersp, on 13 Aug, 2008 - 11:15 AM, said:

I think this may be a non-code question.

If so, then I hope this will prove helpful...its cool either way:

in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.

10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010

just to simplify you forgot to add remainders >=5 = 1
<=5 = 0

good stuff

### #11 JeroenFM

Reputation: 18
• Posts: 195
• Joined: 30-June 08

## Re: converting binary to decimal

Posted 19 August 2008 - 02:18 AM

Ak-Emm, on 18 Aug, 2008 - 08:08 AM, said:

just to simplify you forgot to add remainders >=5 = 1
<=5 = 0

good stuff

Clearly you didn't understand his method, the remainder of any positive number divided by 2 is always either 0 or 1, so it's always less than 5.

Try this:

```public int remainderOfDiv2(int number) {
return number % 2;
}

```

Pass any positive number, if you find one that yields something other than 1 or 0 let me know.

This post has been edited by JeroenFM: 19 August 2008 - 02:22 AM