ByteUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.andy.kads.utils;
  2. import javax.xml.bind.DatatypeConverter;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. /**
  6. * Created by IntelliJ IDEA.
  7. * User: andy.qu
  8. * Date: 2019/8/20
  9. */
  10. public class ByteUtil {
  11. public static String arrayShow(byte[] b1) {
  12. return Arrays.toString(b1);
  13. }
  14. public static byte[] hexString2ByteArray(String s1) {
  15. if(s1 == null || s1.length() == 0){
  16. return null;
  17. }
  18. s1 = s1.replaceAll(" ", "").toUpperCase();
  19. return DatatypeConverter.parseHexBinary(s1);
  20. }
  21. public static String byteArray2HexString(byte[] b1) {
  22. return DatatypeConverter.printHexBinary(b1);
  23. }
  24. public static byte[] string2byteArray(String s1) {
  25. return s1.getBytes();
  26. }
  27. public static String byteArray2String(byte[] b1) {
  28. if (b1 != null)
  29. return new String(b1);
  30. else
  31. return "";
  32. }
  33. public static int string2Int(String s1, int n1) {
  34. return Integer.parseInt(s1, n1);
  35. }
  36. public static String ascii2Bin(String s1) {
  37. return byteArray2String(hexString2ByteArray(s1));
  38. }
  39. public static String bin2Ascii(String s1) {
  40. return byteArray2HexString(string2byteArray(s1));
  41. }
  42. public static byte[] int2byte2(int i) {
  43. byte[] a = new byte[2];
  44. a[1] = (byte) (0xff & i);
  45. a[0] = (byte) ((0xff00 & i) >> 8);
  46. //a[2] = (byte) ((0xff0000 & i) >> 16);
  47. //a[3] = (byte) ((0xff000000 & i) >> 24);
  48. //a 为转换后的byte
  49. for (int j = 0; j < a.length; j++) {
  50. System.out.println(a[j]);
  51. }
  52. return a;
  53. }
  54. /**
  55. * 把b2的内容复制到b1指定位置
  56. *
  57. * @param sp
  58. * @param b1
  59. * @param b2
  60. * @return
  61. */
  62. public static byte[] cpbyte(int sp, byte[] b1, byte[] b2) {
  63. //byte[] code=.int2byte4(fcode);
  64. for (int i = 0; i < b2.length; i++) {
  65. b1[i + sp] = b2[i];
  66. }
  67. return b1;
  68. }
  69. public static byte[] concat(byte[] a, byte[] b) {
  70. byte[] c = new byte[a.length + b.length];
  71. for (int i = 0; i < a.length; i++) {
  72. c[i] = a[i];
  73. }
  74. for (int i = 0; i + a.length < c.length; i++) {
  75. c[i + a.length] = b[i];
  76. }
  77. return c;
  78. }
  79. public String int2DoubleStr(int i) {
  80. if (i >= 0) {
  81. int j = i % 100;
  82. if (j < 0) {
  83. return "";
  84. }
  85. if (j < 10) {
  86. return i / 100 + ".0" + j;
  87. } else {
  88. return i / 100 + "." + j;
  89. }
  90. } else {
  91. int j = -i % 100;
  92. if (j < 0) {
  93. return "";
  94. }
  95. if (i / 100 == 0) {
  96. if (j < 10) {
  97. return "-" + i / 100 + ".0" + j;
  98. } else {
  99. return "-" + i / 100 + "." + j;
  100. }
  101. } else {
  102. if (j < 10) {
  103. return i / 100 + ".0" + j;
  104. } else {
  105. return i / 100 + "." + j;
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * 通过byte数组取到无符号short
  112. * @param b
  113. * @return
  114. * @throws IOException
  115. */
  116. public static int getUnsignedShort(byte b[], int index){
  117. return (b[index+0] << 8& 0xFF00) + (b[index+1]<< 0& 0xFF);
  118. }
  119. /**
  120. * 通过byte数组取到short
  121. *
  122. * @param b
  123. * @param index
  124. * 第几位开始取
  125. * @return
  126. */
  127. public static short getShort(byte[] b, int index) {
  128. return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
  129. }
  130. public static short getShortSmallEndia(byte[] b, int index) {
  131. return (short) (((b[index + 1] << 8) | b[index ] & 0xff));
  132. }
  133. /**
  134. * 通过byte数组取到int
  135. *
  136. * @param bb
  137. * @param index
  138. * 第几位开始
  139. * @return
  140. */
  141. public static int getInt(byte[] bb, int index) {
  142. return (int) ((((bb[index + 0] & 0xff) << 24)
  143. | ((bb[index + 1] & 0xff) << 16)
  144. | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
  145. }
  146. /**
  147. * 16进制转ASCII
  148. *
  149. * @param hex
  150. * @return
  151. */
  152. public static String hex2Str(String hex) {
  153. StringBuilder sb = new StringBuilder();
  154. StringBuilder temp = new StringBuilder();
  155. //49204c6f7665204a617661 split into two characters 49, 20, 4c...
  156. for (int i = 0; i < hex.length() - 1; i += 2) {
  157. //grab the hex in pairs
  158. String output = hex.substring(i, (i + 2));
  159. //convert hex to decimal
  160. int decimal = Integer.parseInt(output, 16);
  161. //convert the decimal to character
  162. sb.append((char) decimal);
  163. temp.append(decimal);
  164. }
  165. return sb.toString();
  166. }
  167. private static byte toByte(char c){
  168. byte b = (byte)"0123456789abcdef".indexOf(c);
  169. return b;
  170. }
  171. public static byte[] hexStringToByte(String hex){
  172. int len = (hex.length()/2);
  173. byte[] result = new byte[len];
  174. char[] achar = hex.toCharArray();
  175. for(int i=0;i<len;i++){
  176. int pos = i*2;
  177. result[i] = (byte)(toByte(achar[pos])<< 4 | toByte(achar[pos+1]));
  178. }
  179. return result;
  180. }
  181. /**
  182. * 16进制字符串转2进制字符串
  183. */
  184. public static String hexString2binaryString(String hexString) {
  185. if (hexString == null || hexString.length() % 2 != 0)
  186. return null;
  187. String bString = "", tmp;
  188. for (int i = 0; i < hexString.length(); i++) {
  189. tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
  190. bString += tmp.substring(tmp.length() - 4);
  191. }
  192. return bString;
  193. //return Integer.toBinaryString(Integer.parseInt(hexString, 16));
  194. }
  195. public static void main(String[] args) {
  196. String str = "001205";
  197. byte[] bytes = DatatypeConverter.parseHexBinary(str);
  198. System.out.println((bytes[3] & 0xff));
  199. //System.out.println(ByteUtil.getUnsignedShort(bytes,4));
  200. }
  201. }