Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Converting binary to decimal

Greenhorn
Posts: 1
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

Ranch Hand
Posts: 2410
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

Sheriff
Posts: 11343

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: 2410
Oops. Thanks Marc
[ March 07, 2006: Message edited by: Keith Lynn ]

 Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS