Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String [] args)
- {
- int test = BinaryToDecimal("10011011", 0);
- System.out.print(test);
- }
- public static int BinaryToDecimal(String decimal, int location)
- {
- if(decimal.length() == 0)
- {
- return 0;
- }
- else
- {
- if( decimal.charAt(decimal.length()-1) == '1')
- {
- return (int)Math.pow(2, location) + BinaryToDecimal(decimal.substring(0,decimal.length()-1), location+1);
- }
- else{
- return BinaryToDecimal(decimal.substring(0,decimal.length()-1), location+1);
- }
- }
- }
Add Comment
Please, Sign In to add comment