How to Convert Binary to Decimal in Java

Save

Java is a high-level, object-oriented programming language created by Sun Microsystems. In addition to allowing programs to define their own data types, or "classes," Java includes a collection of predefined types. When a numerical value is expressed in a base different than 10, or "decimal," you need to parse that value into one of Java's numerical types. In particular, you can convert binary (base 2) values to decimal using methods included in the standard Java platform.

  • Obtain the value represented in binary, and store the binary representation in a string. This sample code stores the binary representation of 192:

    String binNumber = "11000000";

  • Convert the string to an integer using the built-in parseInt() method, as in this sample code:

    int number = Integer.parseInt(binNumber,2);

  • Generate the decimal representation of the number, as in this sample code:

    String decNumber = String.format("%d",number);

    For the example, the string "decNumber" will contain the value "192."

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!