java字节数组byte[],16进制字符串互转,

迷南。 2022-04-06 02:42 530阅读 0赞

commons-codec-*.jar里有通用方法

Hex.encodeHex(byte[]) 字节数组转换成16进制字符串 (可转成大写的)

Hex.decodeHex(char[]) 16进制“字符串”转换成字节数组 (支持大写的)

下面的两个是自定义写法,建议用通用方法。

  1. import org.apache.commons.codec.DecoderException;
  2. import org.apache.commons.codec.binary.Hex;
  3. /**
  4. * 字符串工具类
  5. * @author happyqing
  6. * @since 2013.11.6
  7. */
  8. public class StringUtil {
  9. /**
  10. * 字节数组转换成16进制字符串
  11. * @param bytes 字节数组
  12. * @return 16进制字符串
  13. */
  14. public static String hexEncode(byte[] bytes) {
  15. if (bytes == null || bytes.length <= 0) {
  16. return null;
  17. }
  18. return new String(Hex.encodeHex(bytes)); //Hex.encodeHex(bytes, false)
  19. }
  20. /**
  21. * 16进制字符串转换成字节数组
  22. * @param hexStr 16进制字符串
  23. * @return 字节数组
  24. */
  25. public static byte[] hexDecode(String hexStr) {
  26. if (hexStr == null || "".equals(hexStr)) {
  27. return null;
  28. }
  29. try {
  30. char[] cs = hexStr.toCharArray();
  31. return Hex.decodeHex(cs);
  32. } catch (DecoderException e) {
  33. e.printStackTrace();
  34. }
  35. return null;
  36. }
  37. /**
  38. * 字节数组转为16进制字符串
  39. * @param bytes 字节数组
  40. * @return 16进制字符串
  41. */
  42. public static String byteArray2HexString(byte[] bytes) {
  43. if (bytes == null || bytes.length <= 0) {
  44. return null;
  45. }
  46. //先把byte[] 转换维char[],再把char[]转换为字符串
  47. char[] chars = new char[bytes.length * 2]; // 每个byte对应两个字符
  48. final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  49. for (int i = 0, j = 0; i < bytes.length; i++) {
  50. chars[j++] = hexDigits[bytes[i] >> 4 & 0x0f]; // 先存byte的高4位
  51. chars[j++] = hexDigits[bytes[i] & 0x0f]; // 再存byte的低4位
  52. }
  53. return new String(chars);
  54. }
  55. /**
  56. * 16进制字符串转字节数组
  57. * @param hexString 16进制字符串
  58. * @return 字节数组
  59. */
  60. public static byte[] hexString2ByteArray(String hexString) {
  61. if (hexString == null || "".equals(hexString)) {
  62. return null;
  63. }
  64. //先把字符串转换为char[],再转换为byte[]
  65. int length = hexString.length() / 2;
  66. char[] hexChars = hexString.toCharArray();
  67. byte[] bytes = new byte[length];
  68. String hexDigits = "0123456789abcdef";
  69. for (int i = 0; i < length; i++) {
  70. int pos = i * 2; // 两个字符对应一个byte
  71. int h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1
  72. int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2
  73. if (h == -1 || l == -1) { // 非16进制字符
  74. return null;
  75. }
  76. bytes[i] = (byte) (h | l);
  77. }
  78. return bytes;
  79. }
  80. /**
  81. * @param args
  82. */
  83. public static void main(String[] args) {
  84. //String str = "15811111111";
  85. String str = "字节数组转16进制字符串";
  86. String result = null;
  87. byte[] bytes = null;
  88. long b = System.currentTimeMillis();
  89. for(int i=0; i<10000; i++){
  90. //result = byteArray2HexString(str.getBytes()); //27
  91. result = hexEncode(str.getBytes()); //32
  92. }
  93. System.out.println("耗时:"+(System.currentTimeMillis()-b));
  94. System.out.println("result:"+result);
  95. long b2 = System.currentTimeMillis();
  96. for(int i=0; i<10000; i++){
  97. //bytes = hexString2ByteArray(result); //32
  98. bytes = hexDecode(result); //16
  99. }
  100. System.out.println("耗时:"+(System.currentTimeMillis()-b2));
  101. System.out.println("result:"+new String(bytes));
  102. }
  103. }

发表评论

表情:
评论列表 (有 0 条评论,530人围观)

还没有评论,来说两句吧...

相关阅读