Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. package net.coderodde.util;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ByteStringConverter {
  6.  
  7. /**
  8. * Converts the given byte array to its textual hex representation.
  9. * @param bytes the byte array to stringify.
  10. * @return the string representing the input byte array.
  11. */
  12. public static String convertByteArrayToHexString(byte[] bytes) {
  13. StringBuilder stringBuilder = new StringBuilder(2 * bytes.length);
  14.  
  15. for (byte b : bytes) {
  16. stringBuilder.append(convertByteToHexString(b));
  17. }
  18.  
  19. return stringBuilder.toString();
  20. }
  21.  
  22. /**
  23. * Converts the given hex string to the byte array it represents.
  24. * @param hexString the hex string to convert.
  25. * @return the byte array represented by the input hex string.
  26. */
  27. public static byte[] convertHexStringToByteArray(String hexString) {
  28. byte[] byteArray = new byte[hexString.length() / 2];
  29.  
  30. for (int i = 0; i < hexString.length(); i += 2) {
  31. byteArray[i / 2] = convertHexByteStringToByte(
  32. hexString.substring(i, i + 2));
  33. }
  34.  
  35. return byteArray;
  36. }
  37.  
  38. /**
  39. * Converts the input character {@code c} to the nibble it represents. This
  40. * method assumes that {@code c} is numeric or within range
  41. * {@code a, b, c, d, e, f}.
  42. * @param c the character to convert to a nibble.
  43. * @return the byte value of the textual representation of a nibble.
  44. */
  45. private static byte convertHexCharToNibble(char c) {
  46. c = Character.toLowerCase(c);
  47.  
  48. switch (c) {
  49. case '0':
  50. return 0;
  51. case '1':
  52. return 1;
  53. case '2':
  54. return 2;
  55. case '3':
  56. return 3;
  57. case '4':
  58. return 4;
  59. case '5':
  60. return 5;
  61. case '6':
  62. return 6;
  63. case '7':
  64. return 7;
  65. case '8':
  66. return 8;
  67. case '9':
  68. return 9;
  69. case 'a':
  70. return 10;
  71. case 'b':
  72. return 11;
  73. case 'c':
  74. return 12;
  75. case 'd':
  76. return 13;
  77. case 'e':
  78. return 14;
  79. case 'f':
  80. return 15;
  81.  
  82. default:
  83. throw new IllegalArgumentException("Not a hex digit: " + c);
  84. }
  85. }
  86.  
  87. /**
  88. * Converts the input hex byte string to the byte it represents.
  89. * @param hexByteString the hex byte string to convert.
  90. * @return the byte value represented by {@code hexByteString}.
  91. */
  92. private static byte convertHexByteStringToByte(String hexByteString) {
  93. char lo = hexByteString.charAt(1);
  94. char hi = hexByteString.charAt(0);
  95. byte lob = convertHexCharToNibble(lo);
  96. byte hib = convertHexCharToNibble(hi);
  97. return (byte)((hib << 4) | lob);
  98. }
  99.  
  100. /**
  101. * Converts the given byte to its textual hex representation.
  102. * @param b the byte to convert.
  103. * @return the textual representation of the byte {@code b}.
  104. */
  105. private static String convertByteToHexString(byte b) {
  106. byte lo = (byte)(b & (byte) 0xf);
  107. byte hi = (byte)((b >>> 4) & (byte) 0xf);
  108. StringBuilder stringBuilder = new StringBuilder(2);
  109. appendNibbleToStringBuilder(stringBuilder, hi);
  110. appendNibbleToStringBuilder(stringBuilder, lo);
  111. return stringBuilder.toString();
  112. }
  113.  
  114. /**
  115. * Appends the textual representation of {@code nibble} to
  116. * {@code stringBuilder}.
  117. * @param stringBuilder the target string builder.
  118. * @param nibble the nibble to append.
  119. */
  120. private static void appendNibbleToStringBuilder(StringBuilder stringBuilder,
  121. byte nibble) {
  122. switch (nibble) {
  123. case 0:
  124. stringBuilder.append('0');
  125. break;
  126. case 1:
  127. stringBuilder.append('1');
  128. break;
  129. case 2:
  130. stringBuilder.append('2');
  131. break;
  132. case 3:
  133. stringBuilder.append('3');
  134. break;
  135. case 4:
  136. stringBuilder.append('4');
  137. break;
  138. case 5:
  139. stringBuilder.append('5');
  140. break;
  141. case 6:
  142. stringBuilder.append('6');
  143. break;
  144. case 7:
  145. stringBuilder.append('7');
  146. break;
  147. case 8:
  148. stringBuilder.append('8');
  149. break;
  150. case 9:
  151. stringBuilder.append('9');
  152. break;
  153. case 10:
  154. stringBuilder.append('a');
  155. break;
  156. case 11:
  157. stringBuilder.append('b');
  158. break;
  159. case 12:
  160. stringBuilder.append('c');
  161. break;
  162. case 13:
  163. stringBuilder.append('d');
  164. break;
  165. case 14:
  166. stringBuilder.append('e');
  167. break;
  168. case 15:
  169. stringBuilder.append('f');
  170. break;
  171. }
  172. }
  173.  
  174. public static void main(String[] args) {
  175. Scanner scanner = new Scanner(System.in);
  176.  
  177. while (true) {
  178. String hexString = scanner.next();
  179. byte[] hexBytes = convertHexStringToByteArray(hexString);
  180. String newHexString = convertByteArrayToHexString(hexBytes);
  181. System.out.println(newHexString);
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement