Guest User

Untitled

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. public static void main(String [] args)
  2. {
  3. int test = BinaryToDecimal("10011011", 0);
  4. System.out.print(test);
  5. }
  6.  
  7. public static int BinaryToDecimal(String decimal, int location)
  8. {
  9. if(decimal.length() == 0)
  10. {
  11. return 0;
  12. }
  13. else
  14. {
  15. if( decimal.charAt(decimal.length()-1) == '1')
  16. {
  17. return (int)Math.pow(2, location) + BinaryToDecimal(decimal.substring(0,decimal.length()-1), location+1);
  18. }
  19. else{
  20. return BinaryToDecimal(decimal.substring(0,decimal.length()-1), location+1);
  21. }
  22. }
  23. }
Add Comment
Please, Sign In to add comment