Byte数组相关工具类

左手的ㄟ右手 2024-04-07 13:40 146阅读 0赞

byte 转换基本数据类型

  1. kotlin 中可以直接调用 toByte , toInt 之类的函数直接获取。
  2. java 中需要自己获取,获取方式会放到工具类中。

大端和小端

下面是对连个模式的简单解释。

  • 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于 把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;这和我们的阅读习惯一致。
  • 小端模式:是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中。

更详细的可以参考这篇博客,详解大端模式和小端模式。

1、函数列表

  1. int2BytesBig :将int转为高字节在前,低字节在后的byte数组(大端)
  2. int2BytesLittle :将int转为低字节在前,高字节在后的byte数组(小端)
  3. bytes2IntLittle :byte数组到int的转换(小端)
  4. bytes2IntBig :byte数组到int的转换(大端)
  5. short2BytesBig :将short转为高字节在前,低字节在后的byte数组(大端)
  6. short2BytesLittle :将short转为低字节在前,高字节在后的byte数组(小端)
  7. bytes2ShortLittle :读取小端byte数组为short
  8. bytes2ShortBig :读取大端byte数组为short
  9. long2BytesBig :long类型转byte[] (大端)
  10. long2BytesLittle :long类型转byte[] (小端)
  11. bytes2LongLittle :byte[]转long类型(小端)
  12. bytes2LongBig :byte[]转long类型(大端)
  13. long2BytesBOS :long类型转byte[] (大端),通过ByteArrayOutputStream实现
  14. bytes2LongBOS :byte[]转long类型(大端),通过ByteArrayOutputStream实现
  15. long2BytesByteBuffer:long类型转byte[] (大端),通过ByteBuffer实现
  16. bytes2LongByteBuffer:byte[]转long类型(大端), 通过ByteBuffer实现
  17. bytes2Long :将字节数组转为long,支持大小端
  18. bytes2LongByteBuffer:将字节数组转为long,支持大小端
  19. int2Byte :int byte
  20. byte2Int :byte int,解决javabyte输出可能为负数的问题
  21. object2Bytes :将 object --> byte 数组
  22. bytes2Object :数组转对象
  23. bytesEquals :两个byte[]是否相同
  24. subBytes :截取byte[]
  25. bytesMerger :拼接byte[] byte[]
  26. bytesMerger :拼接byte byte[]
  27. bytesMerger :拼接byte[] byte
  28. bytesMerger :拼接三个数组
  29. byte2Hex :byteHex 16 进制字符串
  30. bytes2Hex :byte[]转Hex
  31. bytes2Hex2 :字节数组转为16进制字符串
  32. hex2Byte :Hexbyte,hex只能含两个字符,如果是一个字符byte高位会是0
  33. hex2Bytes :Hexbyte[],两种情况,Hex长度为奇数最后一个字符会被舍去
  34. str2HexStr :字符串转为16进制字符串,推荐
  35. hexStr2Str :16进制直接转换成为字符串(无需Unicode解码),推荐
  36. string2Unicode :字符串转换unicode
  37. unicode2String :unicode 转字符串

2、工具类

  1. public class BytesUtils {
  2. /**
  3. * 将int转为高字节在前,低字节在后的byte数组(大端)
  4. *
  5. * @param n int
  6. * @return byte[]
  7. */
  8. public static byte[] int2BytesBig(int n) {
  9. byte[] b = new byte[4];
  10. b[3] = (byte) (n & 0xff);
  11. b[2] = (byte) (n >> 8 & 0xff);
  12. b[1] = (byte) (n >> 16 & 0xff);
  13. b[0] = (byte) (n >> 24 & 0xff);
  14. return b;
  15. }
  16. /**
  17. * 将int转为低字节在前,高字节在后的byte数组(小端)
  18. *
  19. * @param n int
  20. * @return byte[]
  21. */
  22. public static byte[] int2BytesLittle(int n) {
  23. byte[] b = new byte[4];
  24. b[0] = (byte) (n & 0xff);
  25. b[1] = (byte) (n >> 8 & 0xff);
  26. b[2] = (byte) (n >> 16 & 0xff);
  27. b[3] = (byte) (n >> 24 & 0xff);
  28. return b;
  29. }
  30. /**
  31. * byte数组到int的转换(小端)
  32. *
  33. * @param bytes
  34. * @return
  35. */
  36. public static int bytes2IntLittle(byte[] bytes) {
  37. int int1 = bytes[0] & 0xff;
  38. int int2 = (bytes[1] & 0xff) << 8;
  39. int int3 = (bytes[2] & 0xff) << 16;
  40. int int4 = (bytes[3] & 0xff) << 24;
  41. return int1 | int2 | int3 | int4;
  42. }
  43. /**
  44. * byte数组到int的转换(大端)
  45. *
  46. * @param bytes
  47. * @return
  48. */
  49. public static int bytes2IntBig(byte[] bytes) {
  50. int int1 = bytes[3] & 0xff;
  51. int int2 = (bytes[2] & 0xff) << 8;
  52. int int3 = (bytes[1] & 0xff) << 16;
  53. int int4 = (bytes[0] & 0xff) << 24;
  54. return int1 | int2 | int3 | int4;
  55. }
  56. /**
  57. * 将short转为高字节在前,低字节在后的byte数组(大端)
  58. *
  59. * @param n short
  60. * @return byte[]
  61. */
  62. public static byte[] short2BytesBig(short n) {
  63. byte[] b = new byte[2];
  64. b[1] = (byte) (n & 0xff);
  65. b[0] = (byte) (n >> 8 & 0xff);
  66. return b;
  67. }
  68. /**
  69. * 将short转为低字节在前,高字节在后的byte数组(小端)
  70. *
  71. * @param n short
  72. * @return byte[]
  73. */
  74. public static byte[] short2BytesLittle(short n) {
  75. byte[] b = new byte[2];
  76. b[0] = (byte) (n & 0xff);
  77. b[1] = (byte) (n >> 8 & 0xff);
  78. return b;
  79. }
  80. /**
  81. * 读取小端byte数组为short
  82. *
  83. * @param b
  84. * @return
  85. */
  86. public static short bytes2ShortLittle(byte[] b) {
  87. return (short) (((b[1] << 8) | b[0] & 0xff));
  88. }
  89. /**
  90. * 读取大端byte数组为short
  91. *
  92. * @param b
  93. * @return
  94. */
  95. public static short bytes2ShortBig(byte[] b) {
  96. return (short) (((b[0] << 8) | b[1] & 0xff));
  97. }
  98. /**
  99. * long类型转byte[] (大端)
  100. *
  101. * @param n
  102. * @return
  103. */
  104. public static byte[] long2BytesBig(long n) {
  105. byte[] b = new byte[8];
  106. b[7] = (byte) (n & 0xff);
  107. b[6] = (byte) (n >> 8 & 0xff);
  108. b[5] = (byte) (n >> 16 & 0xff);
  109. b[4] = (byte) (n >> 24 & 0xff);
  110. b[3] = (byte) (n >> 32 & 0xff);
  111. b[2] = (byte) (n >> 40 & 0xff);
  112. b[1] = (byte) (n >> 48 & 0xff);
  113. b[0] = (byte) (n >> 56 & 0xff);
  114. return b;
  115. }
  116. /**
  117. * long类型转byte[] (小端)
  118. *
  119. * @param n
  120. * @return
  121. */
  122. public static byte[] long2BytesLittle(long n) {
  123. byte[] b = new byte[8];
  124. b[0] = (byte) (n & 0xff);
  125. b[1] = (byte) (n >> 8 & 0xff);
  126. b[2] = (byte) (n >> 16 & 0xff);
  127. b[3] = (byte) (n >> 24 & 0xff);
  128. b[4] = (byte) (n >> 32 & 0xff);
  129. b[5] = (byte) (n >> 40 & 0xff);
  130. b[6] = (byte) (n >> 48 & 0xff);
  131. b[7] = (byte) (n >> 56 & 0xff);
  132. return b;
  133. }
  134. /**
  135. * byte[]转long类型(小端)
  136. *
  137. * @param array
  138. * @return
  139. */
  140. public static long bytes2LongLittle(byte[] array) {
  141. return ((((long) array[0] & 0xff))
  142. | (((long) array[1] & 0xff) << 8)
  143. | (((long) array[2] & 0xff) << 16)
  144. | (((long) array[3] & 0xff) << 24)
  145. | (((long) array[4] & 0xff) << 32)
  146. | (((long) array[5] & 0xff) << 40)
  147. | (((long) array[6] & 0xff) << 48)
  148. | (((long) array[7] & 0xff) << 56));
  149. }
  150. /**
  151. * byte[]转long类型(大端)
  152. *
  153. * @param array
  154. * @return
  155. */
  156. public static long bytes2LongBig(byte[] array) {
  157. return ((((long) array[0] & 0xff) << 56)
  158. | (((long) array[1] & 0xff) << 48)
  159. | (((long) array[2] & 0xff) << 40)
  160. | (((long) array[3] & 0xff) << 32)
  161. | (((long) array[4] & 0xff) << 24)
  162. | (((long) array[5] & 0xff) << 16)
  163. | (((long) array[6] & 0xff) << 8)
  164. | (((long) array[7] & 0xff)));
  165. }
  166. /**
  167. * long类型转byte[] (大端),通过ByteArrayOutputStream实现
  168. *
  169. * @param l
  170. * @return
  171. * @throws IOException
  172. */
  173. public static byte[] long2BytesBOS(long l) throws IOException {
  174. ByteArrayOutputStream bao = new ByteArrayOutputStream();
  175. DataOutputStream dos = new DataOutputStream(bao);
  176. dos.writeLong(l);
  177. byte[] buf = bao.toByteArray();
  178. return buf;
  179. }
  180. /**
  181. * byte[]转long类型(大端),通过ByteArrayOutputStream实现
  182. *
  183. * @param data
  184. * @return
  185. * @throws IOException
  186. */
  187. public long bytes2LongBOS(byte[] data) throws IOException {
  188. ByteArrayInputStream bai = new ByteArrayInputStream(data);
  189. DataInputStream dis = new DataInputStream(bai);
  190. return dis.readLong();
  191. }
  192. /**
  193. * long类型转byte[] (大端),通过ByteBuffer实现
  194. *
  195. * @param value
  196. * @return
  197. */
  198. public static byte[] long2BytesByteBuffer(long value) {
  199. return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(value).array();
  200. }
  201. /**
  202. * byte[]转long类型(大端), 通过ByteBuffer实现
  203. *
  204. * @param bytes
  205. * @return
  206. */
  207. public static long bytes2LongByteBuffer(byte[] bytes) {
  208. ByteBuffer buffer = ByteBuffer.allocate(8);
  209. buffer.put(bytes, 0, bytes.length);
  210. buffer.flip();
  211. return buffer.getLong();
  212. }
  213. /**
  214. * 将字节数组转为long<br>
  215. * 如果input为null,或offset指定的剩余数组长度不足8字节则抛出异常
  216. *
  217. * @param input byte[]
  218. * @param offset 起始偏移量 0
  219. * @param littleEndian 输入数组是否小端模式
  220. * @return
  221. */
  222. public static long bytes2Long(byte[] input, int offset, boolean littleEndian) {
  223. long value = 0;
  224. // 循环读取每个字节通过移位运算完成long的8个字节拼装
  225. for (int count = 0; count < 8; ++count) {
  226. int shift = (littleEndian ? count : (7 - count)) << 3;
  227. value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
  228. }
  229. return value;
  230. }
  231. /**
  232. * 利用 {@link java.nio.ByteBuffer}实现byte[]转long
  233. *
  234. * @param input byte[]
  235. * @param offset 0
  236. * @param littleEndian 输入数组是否小端模式
  237. * @return
  238. */
  239. public static long bytes2LongByteBuffer(byte[] input, int offset, boolean littleEndian) {
  240. // 将byte[] 封装为 ByteBuffer
  241. ByteBuffer buffer = ByteBuffer.wrap(input, offset, 8);
  242. if (littleEndian) {
  243. // ByteBuffer.order(ByteOrder) 方法指定字节序,即大小端模式(BIG_ENDIAN/LITTLE_ENDIAN)
  244. // ByteBuffer 默认为大端(BIG_ENDIAN)模式
  245. buffer.order(ByteOrder.LITTLE_ENDIAN);
  246. }
  247. return buffer.getLong();
  248. }
  249. /**
  250. * int 转 byte
  251. *
  252. * @param t
  253. * @return
  254. */
  255. public static byte int2Byte(int t) {
  256. return (byte) t;
  257. }
  258. /**
  259. * byte 转 int,解决java中byte输出可能为负数的问题
  260. *
  261. * @param b
  262. * @return
  263. */
  264. public static int byte2Int(byte b) {
  265. return b & 0xFF;
  266. }
  267. /**
  268. * 将 object --> byte 数组
  269. *
  270. * @param obj
  271. * @return
  272. */
  273. public static byte[] object2Bytes(Object obj) {
  274. byte[] bytes = null;
  275. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  276. try {
  277. ObjectOutputStream oos = new ObjectOutputStream(bos);
  278. oos.writeObject(obj);
  279. oos.flush();
  280. bytes = bos.toByteArray();
  281. oos.close();
  282. bos.close();
  283. } catch (IOException ex) {
  284. ex.printStackTrace();
  285. }
  286. return bytes;
  287. }
  288. /**
  289. * 数组转对象
  290. *
  291. * @param bytes
  292. * @return
  293. */
  294. public Object bytes2Object(byte[] bytes) {
  295. Object obj = null;
  296. try {
  297. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  298. ObjectInputStream ois = new ObjectInputStream(bis);
  299. obj = ois.readObject();
  300. ois.close();
  301. bis.close();
  302. } catch (IOException | ClassNotFoundException ex) {
  303. ex.printStackTrace();
  304. }
  305. return obj;
  306. }
  307. /**
  308. * 两个byte[]是否相同
  309. * @param data1
  310. * @param data2
  311. * @return
  312. */
  313. public static boolean bytesEquals(byte[] data1, byte[] data2) {
  314. return Arrays.equals(data1, data2);
  315. }
  316. /**
  317. * 截取byte[]
  318. * @param data
  319. * @param position
  320. * @param length
  321. * @return
  322. */
  323. public static byte[] subBytes(byte[] data, int position, int length) {
  324. byte[] temp = new byte[length];
  325. System.arraycopy(data, position, temp, 0, length);
  326. return temp;
  327. }
  328. /**
  329. * 拼接byte[] 和 byte[]
  330. *
  331. * @param bytes1
  332. * @param bytes2
  333. * @return
  334. */
  335. public static byte[] bytesMerger(byte[] bytes1, byte[] bytes2) {
  336. byte[] bytes3 = new byte[bytes1.length + bytes2.length];
  337. System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
  338. System.arraycopy(bytes2, 0, bytes3, bytes1.length, bytes2.length);
  339. return bytes3;
  340. }
  341. /**
  342. * 拼接byte 和 byte[]
  343. *
  344. * @param byte1
  345. * @param bytes2
  346. * @return
  347. */
  348. public static byte[] bytesMerger(byte byte1, byte[] bytes2) {
  349. byte[] bytes3 = new byte[1 + bytes2.length];
  350. bytes3[0] = byte1;
  351. System.arraycopy(bytes2, 0, bytes3, 1, bytes2.length);
  352. return bytes3;
  353. }
  354. /**
  355. * 拼接byte[] 和 byte
  356. *
  357. * @param bytes1
  358. * @param byte2
  359. * @return
  360. */
  361. public static byte[] bytesMerger(byte[] bytes1, byte byte2) {
  362. byte[] bytes3 = new byte[1 + bytes1.length];
  363. System.arraycopy(bytes1, 0, bytes3, 0, bytes1.length);
  364. bytes3[bytes3.length - 1] = byte2;
  365. return bytes3;
  366. }
  367. /**
  368. * 拼接三个数组
  369. *
  370. * @param bt1
  371. * @param bt2
  372. * @param bt3
  373. * @return
  374. */
  375. public static byte[] bytesMerger(byte[] bt1, byte[] bt2, byte[] bt3) {
  376. byte[] data = new byte[bt1.length + bt2.length + bt3.length];
  377. System.arraycopy(bt1, 0, data, 0, bt1.length);
  378. System.arraycopy(bt2, 0, data, bt1.length, bt2.length);
  379. System.arraycopy(bt3, 0, data, bt1.length + bt2.length, bt3.length);
  380. return data;
  381. }
  382. /**
  383. * byte转Hex 16 进制字符串
  384. */
  385. public static String byte2Hex(byte b) {
  386. String hex = Integer.toHexString(b & 0xFF);
  387. if (hex.length() < 2) {
  388. hex = "0" + hex;
  389. }
  390. return hex;
  391. }
  392. /**
  393. * byte[]转Hex
  394. */
  395. public static String bytes2Hex(byte[] b) {
  396. StringBuilder sb = new StringBuilder();
  397. for (int i = 0; i < b.length; i++) {
  398. String hex = Integer.toHexString(b[i] & 0xFF);
  399. if (hex.length() < 2) {
  400. hex = "0" + hex;
  401. }
  402. sb.append(hex.toUpperCase());
  403. }
  404. return sb.toString();
  405. }
  406. /**
  407. * 字节数组转为16进制字符串
  408. */
  409. public static String bytes2Hex2(byte[] bytes) {
  410. String strHex = "";
  411. StringBuilder stringBuilder = new StringBuilder();
  412. for (int n = 0; n < bytes.length; n++) {
  413. strHex = Integer.toHexString(bytes[n] & 0xFF);
  414. stringBuilder.append((strHex.length() == 1) ? "0" + strHex : strHex);
  415. }
  416. return stringBuilder.toString().trim();
  417. }
  418. /**
  419. * Hex转byte,hex只能含两个字符,如果是一个字符byte高位会是0
  420. */
  421. public static byte hex2Byte(String hex) {
  422. return (byte) Integer.parseInt(hex, 16);
  423. }
  424. /**
  425. * Hex转byte[],两种情况,Hex长度为奇数最后一个字符会被舍去
  426. */
  427. public static byte[] hex2Bytes(String hex) {
  428. if (hex.length() < 1) {
  429. return null;
  430. } else {
  431. byte[] result = new byte[hex.length() / 2];
  432. int j = 0;
  433. for (int i = 0; i < hex.length(); i += 2) {
  434. result[j++] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
  435. }
  436. return result;
  437. }
  438. }
  439. /**
  440. * 字符串转为16进制字符串,推荐
  441. */
  442. public static String str2HexStr(String str) {
  443. char[] chars = "0123456789ABCDEF".toCharArray();
  444. StringBuilder sb = new StringBuilder("");
  445. byte[] bs = str.getBytes();
  446. int bit;
  447. for (byte b : bs) {
  448. bit = (b & 0x0f0) >> 4;
  449. sb.append(chars[bit]);
  450. bit = b & 0x0f;
  451. sb.append(chars[bit]);
  452. }
  453. return sb.toString().trim();
  454. }
  455. /**
  456. * 16进制直接转换成为字符串(无需Unicode解码),推荐
  457. *
  458. * @param hexStr
  459. * @return
  460. */
  461. public static String hexStr2Str(String hexStr) {
  462. String str = "0123456789ABCDEF";
  463. char[] hexs = hexStr.toCharArray();
  464. byte[] bytes = new byte[hexStr.length() / 2];
  465. int n;
  466. for (int i = 0; i < bytes.length; i++) {
  467. n = str.indexOf(hexs[2 * i]) * 16;
  468. n += str.indexOf(hexs[2 * i + 1]);
  469. bytes[i] = (byte) (n & 0xff);
  470. }
  471. return new String(bytes);
  472. }
  473. /**
  474. * 字符串转换unicode
  475. */
  476. public static String string2Unicode(String string) {
  477. StringBuilder unicode = new StringBuilder();
  478. for (int i = 0; i < string.length(); i++) {
  479. // 取出每一个字符
  480. char c = string.charAt(i);
  481. // 转换为unicode
  482. unicode.append("\u").append(Integer.toHexString(c));
  483. }
  484. return unicode.toString();
  485. }
  486. /**
  487. * unicode 转字符串
  488. */
  489. public static String unicode2String(String unicode) {
  490. StringBuilder string = new StringBuilder();
  491. String[] hex = unicode.split("\\u");
  492. for (int i = 1; i < hex.length; i++) {
  493. // 转换出每一个代码点
  494. int data = Integer.parseInt(hex[i], 16);
  495. // 追加成string
  496. string.append((char) data);
  497. }
  498. return string.toString();
  499. }
  500. }

还有一种写法:

  1. /**
  2. * <pre>
  3. * author : wushaohong
  4. * date : 2020-05-01
  5. * desc : 字节数组与基本数据类型的转换
  6. * byte、short、int、float、long、double、16进制字符串
  7. * version: 1.0
  8. * </pre>
  9. */
  10. public class ByteArrayUtil {
  11. /**
  12. * 字节数组转 short,小端
  13. */
  14. public static short byteArray2Short_Little_Endian(byte[] array) {
  15. // 数组长度有误
  16. if (array.length > 2) {
  17. return 0;
  18. }
  19. short value = 0;
  20. for (int i = 0; i < array.length; i++) {
  21. // & 0xff,除去符号位干扰
  22. value |= ((array[i] & 0xff) << (i * 8));
  23. }
  24. return value;
  25. }
  26. /**
  27. * 字节数组转 short,大端
  28. */
  29. public static short byteArray2Short_Big_Endian(byte[] array) {
  30. // 数组长度有误
  31. if (array.length > 2) {
  32. return 0;
  33. }
  34. short value = 0;
  35. for (int i = 0; i < array.length ; i++) {
  36. value |= ((array[i] & 0xff) << ((array.length - i - 1) * 8));
  37. }
  38. return value;
  39. }
  40. /**
  41. * 字节数组转 int,小端
  42. */
  43. public static int byteArray2Int_Little_Endian(byte[] array) {
  44. // 数组长度有误
  45. if (array.length > 4) {
  46. return 0;
  47. }
  48. int value = 0;
  49. for (int i = 0; i < array.length; i++) {
  50. value |= ((array[i] & 0xff) << (i * 8));
  51. }
  52. return value;
  53. }
  54. /**
  55. * 字节数组转 int,大端
  56. */
  57. public static int byteArray2Int_Big_Endian(byte[] array) {
  58. // 数组长度有误
  59. if (array.length > 4) {
  60. return 0;
  61. }
  62. int value = 0;
  63. for (int i = 0; i < array.length ; i++) {
  64. value |= ((array[i] & 0xff) << ((array.length - i - 1) * 8));
  65. }
  66. return value;
  67. }
  68. /**
  69. * 字节数组转 float,小端
  70. */
  71. public static float byteArray2Float_Little_Endian(byte[] array) {
  72. // 数组长度有误
  73. if (array.length != 4) {
  74. return 0;
  75. }
  76. return Float.intBitsToFloat(byteArray2Int_Little_Endian(array));
  77. }
  78. /**
  79. * 字节数组转 float,大端
  80. */
  81. public static float byteArray2Float_Big_Endian(byte[] array) {
  82. // 数组长度有误
  83. if (array.length > 4) {
  84. return 0;
  85. }
  86. return Float.intBitsToFloat(byteArray2Int_Big_Endian(array));
  87. }
  88. /**
  89. * 字节数组转 long,小端
  90. */
  91. public static long byteArray2Long_Little_Endian(byte[] array) {
  92. // 数组长度有误
  93. if (array.length != 8) {
  94. return 0;
  95. }
  96. long value = 0;
  97. for (int i = 0; i < array.length ; i++) {
  98. // 需要转long再位移,否则int丢失精度
  99. value |= ((long)(array[i]& 0xff) << (i * 8));
  100. }
  101. return value;
  102. }
  103. /**
  104. * 字节数组转 long,大端
  105. */
  106. public static long byteArray2Long_Big_Endian(byte[] array) {
  107. // 数组长度有误
  108. if (array.length != 8) {
  109. return 0;
  110. }
  111. long value = 0;
  112. for (int i = 0; i < array.length ; i++) {
  113. value |= ((long)(array[i] & 0xff) << ((array.length - i - 1) * 8));
  114. }
  115. return value;
  116. }
  117. /**
  118. * 字节数组转 double,小端
  119. */
  120. public static double byteArray2Double_Little_Endian(byte[] array) {
  121. // 数组长度有误
  122. if (array.length != 8) {
  123. return 0;
  124. }
  125. return Double.longBitsToDouble(byteArray2Long_Little_Endian(array));
  126. }
  127. /**
  128. * 字节数组转 double,大端
  129. */
  130. public static double byteArray2Double_Big_Endian(byte[] array) {
  131. // 数组长度有误
  132. if (array.length != 8) {
  133. return 0;
  134. }
  135. return Double.longBitsToDouble(byteArray2Long_Big_Endian(array));
  136. }
  137. /**
  138. * 字节数组转 HexString
  139. */
  140. public static String byteArray2HexString(byte[] array) {
  141. StringBuilder builder = new StringBuilder();
  142. for (byte b : array) {
  143. String s = Integer.toHexString(b & 0xff);
  144. if (s.length() < 2) {
  145. builder.append("0");
  146. }
  147. builder.append(s);
  148. }
  149. return builder.toString().toUpperCase();
  150. }
  151. //---------------------------------华丽的分割线-------------------------------------
  152. /**
  153. * short 转字节数组,小端
  154. */
  155. public static byte[] short2ByteArray_Little_Endian(short s) {
  156. byte[] array = new byte[2];
  157. for (int i = 0; i < array.length; i++) {
  158. array[i] = (byte) (s >> (i * 8));
  159. }
  160. return array;
  161. }
  162. /**
  163. * short 转字节数组,大端
  164. */
  165. public static byte[] short2ByteArray_Big_Endian(short s) {
  166. byte[] array = new byte[2];
  167. for (int i = 0; i < array.length; i++) {
  168. array[array.length - 1 - i] = (byte) (s >> (i * 8));
  169. }
  170. return array;
  171. }
  172. /**
  173. * int 转字节数组,小端
  174. */
  175. public static byte[] int2ByteArray_Little_Endian(int s) {
  176. byte[] array = new byte[4];
  177. for (int i = 0; i < array.length; i++) {
  178. array[i] = (byte) (s >> (i * 8));
  179. }
  180. return array;
  181. }
  182. /**
  183. * int 转字节数组,大端
  184. */
  185. public static byte[] int2ByteArray_Big_Endian(int s) {
  186. byte[] array = new byte[4];
  187. for (int i = 0; i < array.length; i++) {
  188. array[array.length - 1 - i] = (byte) (s >> (i * 8));
  189. }
  190. return array;
  191. }
  192. /**
  193. * float 转字节数组,小端
  194. */
  195. public static byte[] float2ByteArray_Little_Endian(float f) {
  196. return int2ByteArray_Little_Endian(Float.floatToIntBits(f));
  197. }
  198. /**
  199. * float 转字节数组,大端
  200. */
  201. public static byte[] float2ByteArray_Big_Endian(float f) {
  202. return int2ByteArray_Big_Endian(Float.floatToIntBits(f));
  203. }
  204. /**
  205. * long 转字节数组,小端
  206. */
  207. public static byte[] long2ByteArray_Little_Endian(long l) {
  208. byte[] array = new byte[8];
  209. for (int i = 0; i < array.length; i++) {
  210. array[i] = (byte) (l >> (i * 8));
  211. }
  212. return array;
  213. }
  214. /**
  215. * long 转字节数组,大端
  216. */
  217. public static byte[] long2ByteArray_Big_Endian(long l) {
  218. byte[] array = new byte[8];
  219. for (int i = 0; i < array.length; i++) {
  220. array[array.length - 1 - i] = (byte) (l >> (i * 8));
  221. }
  222. return array;
  223. }
  224. /**
  225. * double 转字节数组,小端
  226. */
  227. public static byte[] double2ByteArray_Little_Endian(double d) {
  228. return long2ByteArray_Little_Endian(Double.doubleToLongBits(d));
  229. }
  230. /**
  231. * double 转字节数组,大端
  232. */
  233. public static byte[] double2ByteArray_Big_Endian(double d) {
  234. return long2ByteArray_Big_Endian(Double.doubleToLongBits(d));
  235. }
  236. /**
  237. * HexString 转字节数组
  238. */
  239. public static byte[] hexString2ByteArray(String hexString) {
  240. // 两个十六进制字符一个 byte,单数则有误
  241. if (hexString.length() % 2 != 0) {
  242. return null;
  243. }
  244. byte[] array = new byte[hexString.length() / 2];
  245. int value = 0;
  246. for (int i = 0; i < hexString.length(); i++) {
  247. char s = hexString.charAt(i);
  248. // 前半个字节
  249. if (i % 2 == 0) {
  250. value = Integer.parseInt(String.valueOf(s), 16) * 16;
  251. } else {
  252. // 后半个字节
  253. value += Integer.parseInt(String.valueOf(s), 16);
  254. array[i / 2] = (byte) value;
  255. value = 0;
  256. }
  257. }
  258. return array;
  259. }
  260. }

参考

Java 中 byte、byte 数组和 int、long 之间的转换

大端小端模式

发表评论

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

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

相关阅读