View Full Version : Binary number to a decimal equivalent

03-19-2012, 05:04 AM
Hello, I started java a few weeks ago for my programming class, and I got this assignment where I had to convert a binary number to an integer, or its decimal equivalent. We are not allowed to use shortcuts to convert a binary to a decimal, or import anything other than java.io.*; I know how the binary system works, but I just don't know how to implement it and make it into a program using java. Does anybody have any idea how I can approach this problem? Any help is appreciated thanks.

03-20-2012, 09:25 AM
I know the simple coding,

import java.lang.*;
import java.io.*;
public class DecimalToBinary{
public static void main(String args[]) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal value:");
String hex = bf.readLine();
int i = Integer.parseInt(hex);
String by = Integer.toBinaryString(i);
System.out.println("Binary: " + by);