Advertisement
Guest User

Untitled

a guest
May 27th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class BinToHex {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. String binInput = scanner.nextLine();
  8. switch (binInput.length() % 4) {
  9. case 0: break;
  10. case 1: binInput = "000" + binInput; break;
  11. case 2: binInput = "00" + binInput; break;
  12. case 3: binInput = "0" + binInput; break;
  13. }
  14.  
  15. StringBuilder hexOut = new StringBuilder();
  16.  
  17.  
  18.  
  19. for (int i = 0; i < binInput.length(); i+=4) {
  20. StringBuilder tempBuild = new StringBuilder();
  21. tempBuild = tempBuild.append(binInput.charAt(i));
  22. tempBuild = tempBuild.append(binInput.charAt(i+1));
  23. tempBuild = tempBuild.append(binInput.charAt(i+2));
  24. tempBuild = tempBuild.append(binInput.charAt(i+3));
  25.  
  26. switch (tempBuild.toString()) {
  27. case "0000": hexOut.append("0"); break;
  28. case "0001": hexOut.append("1"); break;
  29. case "0010": hexOut.append("2"); break;
  30. case "0011": hexOut.append("3"); break;
  31. case "0100": hexOut.append("4"); break;
  32. case "0101": hexOut.append("5"); break;
  33. case "0110": hexOut.append("6"); break;
  34. case "0111": hexOut.append("7"); break;
  35. case "1000": hexOut.append("8"); break;
  36. case "1001": hexOut.append("9");; break;
  37. case "1010": hexOut.append("A"); break;
  38. case "1011": hexOut.append("B"); break;
  39. case "1100": hexOut.append("C"); break;
  40. case "1101": hexOut.append("D"); break;
  41. case "1110": hexOut.append("E"); break;
  42. case "1111": hexOut.append("F"); break;
  43. }
  44. }
  45. System.out.println(hexOut);
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement