java - binary to decimal using CharAt( ) and length( ) -
ive written program converting binary decimal, wondering how can without using math.pow ( ) , using methods charat ( ) , length()
thank you,, appreciated
public class binarytodecimal{ public static void main(string[] args){ string binarystring = args[0]; int decimalvalue; int length = binarystring.length(); double j = 0; (int i=0; i< binarystring.length(); i++){ if (binarystring.charat(i)== '1'){ j = j+ math.pow(2, binarystring.length() - 1 - i); } } decimalvalue = (int) j; system.out.println(decimalvalue); } }
you can try this:
int j = 0; (int i=0; i< binarystring.length(); i++) { j <<= 1; if (binarystring.charat(i)== '1') { ++j; } }
Comments
Post a Comment