I know this is very ugly dirty code... But please bear in mind, I'm new to Java myself and I've been up since 5am LOL.. Anyway... Here's what I came up with, it works... There's no error checking so if you don't send in valid binary and not letters, it doesn't check... Have a look at it, see what you think, maybe it'll jog your train of thought....
Code:
public static int binToDec (String b)
{
// Declarations
int string_length = 0;
int loop_control = 0;
int inc_b = 1;
char toconvert = 'a';
int converted = 0;
int return_value = 0;
// Initialise
string_length = b.length();
loop_control = string_length;
// Process binary data
while (loop_control > 0)
{
toconvert = b.charAt((loop_control - 1));
converted = Integer.parseInt(Character.toString(toconvert));
return_value = return_value + (converted * inc_b);
inc_b = inc_b * 2;
loop_control = loop_control - 1;
}
// Return value
return return_value;
}
Call it with that main thing you had...
Bookmarks