help

write a java program to convert binary numbers to decial numbers. the input is a string of zeroes.
[105 byte] By [WAQAS_786a] at [2007-10-3 10:54:37]
# 1
Is this related in any way to http://forum.java.sun.com/thread.jspa?threadID=789587&tstart=0 ?P.S. If the input is a string of zeroes then the output will be zero!
sabre150a at 2007-7-15 6:20:29 > top of Java-index,Other Topics,Algorithms...
# 2

public String toDecimal(String binary) {

return "42";

}

Since you didn't put any restrictions on the conversion algorithm I could implement, I chose an easy one to implement.

DrClapa at 2007-7-15 6:20:29 > top of Java-index,Other Topics,Algorithms...
# 3

@OP: never mind DrClap's solution. Here's mine, which is far better!

public String toDecimal(String binary) {

if("0".equals(binary)) {

return "0";

} else if("1".equals(binary)) {

return "1";

} else if("10".equals(binary)) {

return "2";

} else if("11".equals(binary)) {

return "3";

} else if("100".equals(binary)) {

return "4";

}

// ...

// ... your code here ...

// ...

} else if("1111111111111111111111111111111".equals(binary)) {

return "2147483647";

} else {

return "Hey, that was invalid input!";

}

}

You need to fill in the blanks, of course.

prometheuzza at 2007-7-15 6:20:29 > top of Java-index,Other Topics,Algorithms...
# 4
Do you know how to convert binary to decimal on paper?
CaptainMorgan08a at 2007-7-15 6:20:29 > top of Java-index,Other Topics,Algorithms...