Advertisement
Guest User

lmaoxd

a guest
Nov 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.29 KB | None | 0 0
  1. public int binary2decimal()
  2. {
  3. int sum = 0;
  4. int p = 0;
  5. if(num.substring(0, 1).equals("1"))
  6. {
  7. sum = -128;
  8. }
  9. for(int i = num.length() - 1; i > 0; i--)
  10. {
  11. if(num.substring(i, i+1).equals("1"))
  12. {
  13. sum += Math.pow(2, p);
  14. }
  15. p++;
  16. }
  17. return sum;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement