10 Replies - 4756 Views - Last Post: 19 August 2008 - 02:18 AM Rate Topic: -----

#1 patjoy_24  Icon User is offline

  • New D.I.C Head

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

converting binary to decimal

Post icon  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  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • 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".
Was This Post Helpful? 0
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 eradcoil  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#6 dobbersp  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: converting binary to decimal

Posted 18 August 2008 - 05:45 AM

View Postdobbersp, 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

And for hexadecimal:

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

Was This Post Helpful? 0
  • +
  • -

#8 Ak-Emm  Icon User is offline

  • New D.I.C Head

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

Re: converting binary to decimal

Posted 18 August 2008 - 07:56 AM

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

#9 Ak-Emm  Icon User is offline

  • New D.I.C Head

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

Re: converting binary to decimal

Posted 18 August 2008 - 08:03 AM

View Postpatjoy_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



Oh not bad

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

#10 Ak-Emm  Icon User is offline

  • New D.I.C Head

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

Re: converting binary to decimal

Posted 18 August 2008 - 08:08 AM

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

#11 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: converting binary to decimal

Posted 19 August 2008 - 02:18 AM

View PostAk-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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1