HexUtil.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.fuzamei.util.blockchain;
  2. /**
  3. * Created by zhengfan on 2017/6/23.
  4. * Explain 16进制,字符串等转换
  5. */
  6. public class HexUtil {
  7. /**
  8. * @param b 字节数组
  9. * @return 16进制字符串
  10. * @throws
  11. * @Description:字节数组转16进制字符串
  12. */
  13. public static String bytes2HexString(byte[] b) {
  14. StringBuffer result = new StringBuffer();
  15. String hex;
  16. for (int i = 0; i < b.length; i++) {
  17. hex = Integer.toHexString(b[i] & 0xFF);
  18. if (hex.length() == 1) {
  19. hex = '0' + hex;
  20. }
  21. result.append(hex.toUpperCase());
  22. }
  23. return result.toString();
  24. }
  25. //16进制字符串转字符串
  26. public static String hexToString(String strPart) {
  27. byte[] baKeyword = new byte[strPart.length() / 2];
  28. for (int i = 0; i < baKeyword.length; i++) {
  29. try {
  30. baKeyword[i] = (byte) (0xff & Integer.parseInt(strPart.substring(
  31. i * 2, i * 2 + 2), 16));
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. try {
  37. strPart = new String(baKeyword, "utf-8");// UTF-16le:Not
  38. } catch (Exception e1) {
  39. e1.printStackTrace();
  40. }
  41. return strPart;
  42. }
  43. /**
  44. * @param src 16进制字符串
  45. * @return 字节数组
  46. * @throws
  47. * @Description:16进制字符串转字节数组
  48. */
  49. public static byte[] hexString2Bytes(String src) {
  50. int l = src.length() / 2;
  51. byte[] ret = new byte[l];
  52. for (int i = 0; i < l; i++) {
  53. ret[i] = (byte) Integer
  54. .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
  55. }
  56. return ret;
  57. }
  58. /**
  59. * @param strPart 字符串
  60. * @return 16进制字符串
  61. * @throws
  62. * @Description:字符串转16进制字符串
  63. */
  64. public static String string2HexString(String strPart) {
  65. StringBuffer hexString = new StringBuffer();
  66. for (int i = 0; i < strPart.length(); i++) {
  67. int ch = (int) strPart.charAt(i);
  68. String strHex = Integer.toHexString(ch);
  69. hexString.append(strHex);
  70. }
  71. return hexString.toString();
  72. }
  73. /**
  74. * @param src 16进制字符串
  75. * @return 字节数组
  76. * @throws
  77. * @Title:hexString2String
  78. * @Description:16进制字符串转字符串
  79. */
  80. public static String hexString2String(String src) {
  81. String temp = "";
  82. for (int i = 0; i < src.length() / 2; i++) {
  83. temp = temp
  84. + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),
  85. 16).byteValue();
  86. }
  87. return temp;
  88. }
  89. /**
  90. * @param src
  91. * @return
  92. * @throws
  93. * @Title:char2Byte
  94. * @Description:字符转成字节数据char-->integer-->byte
  95. */
  96. public static Byte char2Byte(Character src) {
  97. return Integer.valueOf((int) src).byteValue();
  98. }
  99. /**
  100. * @param a 转化数据
  101. * @param len 占用字节数
  102. * @return
  103. * @throws
  104. * @Title:intToHexString
  105. * @Description:10进制数字转成16进制
  106. */
  107. private static String intToHexString(int a, int len) {
  108. len <<= 1;
  109. String hexString = Integer.toHexString(a);
  110. int b = len - hexString.length();
  111. if (b > 0) {
  112. for (int i = 0; i < b; i++) {
  113. hexString = "0" + hexString;
  114. }
  115. }
  116. return hexString;
  117. }
  118. public static String bytes2string(byte[] bs) {
  119. char[] cs = new char[bs.length];
  120. for (int p = 0; p < bs.length; p++) {
  121. cs[p] = (char) (bs[p] & 0xFF);
  122. }
  123. return new String(cs);
  124. }
  125. }