HexString.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.tsi.app.common.utils;
  2. import java.nio.ByteBuffer;
  3. import java.util.Objects;
  4. public class HexString {
  5. private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
  6. private HexString() {}
  7. public static String fromByte(byte b) {
  8. return fromBytes(new byte[] {b});
  9. }
  10. public static String fromByte(int b) {
  11. return fromBytes(new byte[] {(byte) b});
  12. }
  13. public static String fromInt(int i) {
  14. byte[] bytes = new byte[] {(byte) (i >> 24), (byte) (i >> 16), (byte) (i >> 8), (byte) (i)};
  15. return fromBytes(bytes);
  16. }
  17. public static String fromLong(long l) {
  18. byte[] bytes =
  19. new byte[] {
  20. (byte) (l >> 56),
  21. (byte) (l >> 48),
  22. (byte) (l >> 40),
  23. (byte) (l >> 32),
  24. (byte) (l >> 24),
  25. (byte) (l >> 16),
  26. (byte) (l >> 8),
  27. (byte) (l)
  28. };
  29. return fromBytes(bytes);
  30. }
  31. public static String fromBytes(byte[] bytes) {
  32. return fromBytes(bytes, 0, bytes.length);
  33. }
  34. public static String fromBytesFormatted(byte[] bytes) {
  35. return fromBytesFormatted(bytes, 0, bytes.length);
  36. }
  37. public static String fromBytes(byte[] bytes, int offset, int length) {
  38. char[] hexChars = new char[length * 2];
  39. for (int j = 0; j < length; j++) {
  40. int v = bytes[j + offset] & 0xff;
  41. hexChars[j * 2] = hexArray[v >>> 4];
  42. hexChars[j * 2 + 1] = hexArray[v & 0x0f];
  43. }
  44. return new String(hexChars);
  45. }
  46. public static String fromBytesSpace(byte[] bytes, int offset, int length) {
  47. char[] hexChars = new char[length * 3];
  48. for (int j = 0; j < length; j++) {
  49. int v = bytes[j + offset] & 0xff;
  50. hexChars[j * 2 ] = hexArray[v >>> 4];
  51. hexChars[j * 2 + 1] = hexArray[v & 0x0f];
  52. hexChars[j * 2 + 2] = ' ';
  53. }
  54. return new String(hexChars);
  55. }
  56. public static String fromBytes(ByteBuffer buffer) {
  57. return fromBytes(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + buffer.limit());
  58. }
  59. public static String fromBytesFormatted(byte[] bytes, int offset, int length) {
  60. StringBuilder builder = new StringBuilder();
  61. int l = 1;
  62. for (int i = offset; i < (offset + length); i++) {
  63. if ((l != 1) && ((l - 1) % 8 == 0)) {
  64. builder.append(' ');
  65. }
  66. if ((l != 1) && ((l - 1) % 16 == 0)) {
  67. builder.append('\n');
  68. }
  69. l++;
  70. appendFromByte(bytes[i], builder);
  71. if (i != offset + length - 1) {
  72. builder.append(' ');
  73. }
  74. }
  75. return builder.toString();
  76. }
  77. public static byte[] toBytes(String hexString) {
  78. Objects.requireNonNull(hexString);
  79. if ((hexString.length() == 0) || ((hexString.length() % 2) != 0)) {
  80. throw new NumberFormatException("argument is not a valid hex string");
  81. }
  82. int length = hexString.length();
  83. byte[] data = new byte[length / 2];
  84. for (int i = 0; i < length; i += 2) {
  85. int firstCharacter = Character.digit(hexString.charAt(i), 16);
  86. int secondCharacter = Character.digit(hexString.charAt(i + 1), 16);
  87. if (firstCharacter == -1 || secondCharacter == -1) {
  88. throw new NumberFormatException("argument is not a valid hex string");
  89. }
  90. data[i / 2] = (byte) ((firstCharacter << 4) + secondCharacter);
  91. }
  92. return data;
  93. }
  94. public static void appendFromByte(byte b, StringBuilder builder) {
  95. builder.append(fromByte(b));
  96. }
  97. public static void appendFromBytes(StringBuilder builder, byte[] bytes, int offset, int length) {
  98. builder.append(fromBytes(bytes, offset, length));
  99. }
  100. }