Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting binary to decimal  RSS feed

 
Ian Avison
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to convert a binary number to decimal. I know Java has built-in functions to do this, but I want to do it manually. Can anyone help me? I don't know where to begin. Thanks
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's best if you start with an example you can work out by hand. You can examine what happens and write code to accomplish it. Then you have a test case to use to give you an idea of whether it works or not. It's generally good to have several test cases.

As an example, consider convert 1010 to decimal.

Each position represents a power of 2.

1010
3210 - are the powers of 2 represented

So as a decimal number 1010 is 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 10
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
...as a decimal number 1010 is 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 10

Typo correction: 1010 is 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Thanks Marc
[ March 07, 2006: Message edited by: Keith Lynn ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!