HexUtil.java 862 B

123456789101112131415161718192021222324252627
  1. package com.ruoyi.common.utils;
  2. public class HexUtil {
  3. private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
  4. public static String bytesToHex(byte[] bytes) {
  5. if (bytes == null) return "null";
  6. char[] hexChars = new char[bytes.length * 2];
  7. for (int j = 0; j < bytes.length; j++) {
  8. int v = bytes[j] & 0xFF;
  9. hexChars[j * 2] = HEX_ARRAY[v >>> 4];
  10. hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
  11. }
  12. return new String(hexChars);
  13. }
  14. public static byte[] hexToBytes(String s) {
  15. int len = s.length();
  16. byte[] data = new byte[len / 2];
  17. for (int i = 0; i < len; i += 2) {
  18. data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
  19. + Character.digit(s.charAt(i+1), 16));
  20. }
  21. return data;
  22. }
  23. }