Advertisement
bleedypeeny

5.9.8 Binary Translator

Mar 5th, 2018
2,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1.  
  2. public String binaryToText(String binary)
  3. {
  4. String ret = "";
  5. // Write your code here!
  6. for( int i=0; i<binary.length(); i+=8)
  7. {
  8. String s = binary.substring(i, i+8);
  9. int dec = binaryToDecimal(s);
  10. char c = (char)dec;
  11. ret += String.valueOf(c);
  12. }
  13. return ret;
  14. }
  15.  
  16.  
  17. public int binaryToDecimal(String binaryString)
  18. {
  19. String[] digits = binaryString.split("");
  20. int res=0;
  21. int exp=digits.length-1;
  22. for(int i=0; i<digits.length; i++)
  23. {
  24. int curDigit = Integer.valueOf(digits[i]).intValue();
  25. res += curDigit * Math.pow(2, exp);
  26. exp --;
  27. }
  28. return res;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement