byte 转换基本数据类型

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

    大端和小端

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

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

函数列表:

  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. byteMerger :合并多个byte成一个byte[]
  26. bytesMerger :拼接多个数组
  27. bytesByteMerger :合并一个数组和多个byte
  28. byteBytesMerger :合并一个byte和多个数组
  29. bytesMerger :拼接多个数组,针对非 byte[]
  30. byte2Hex :byteHex 16 进制字符串
  31. bytes2Hex :byte[]转Hex
  32. bytes2Hex2 :字节数组转为16进制字符串
  33. hex2Byte :Hexbyte,hex只能含两个字符,如果是一个字符byte高位会是0
  34. hex2Bytes :Hexbyte[],两种情况,Hex长度为奇数最后一个字符会被舍去
  35. str2HexStr :字符串转为16进制字符串,推荐
  36. hexStr2Str :16进制直接转换成为字符串(无需Unicode解码),推荐
  37. hex2Str :将16进制数字解码成字符串,适用于所有字符(包括中文)
  38. string2Unicode :字符串转换unicode
  39. unicode2String :unicode 转字符串
  40. str2Bytes :字符串转byte[]
  41. str2Bytes :字符串转byte[],字符集默认是 utf-8
  42. bytes2Str :byte[]转字符串
  43. bytes2Str :byte[]转字符串,字符集默认是 utf-8

工具类

  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. * 将16进制数字解码成字符串,适用于所有字符(包括中文)
  475. *
  476. * @param bytes
  477. * @return
  478. */
  479. public static String hex2Str(String bytes) {
  480. String hexString = "0123456789ABCDEF";
  481. ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
  482. //将每2位16进制整数组装成一个字节
  483. for (int i = 0; i < bytes.length(); i += 2)
  484. baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
  485. return baos.toString();
  486. }
  487. /**
  488. * 字符串转换unicode
  489. */
  490. public static String string2Unicode(String string) {
  491. StringBuilder unicode = new StringBuilder();
  492. for (int i = 0; i < string.length(); i++) {
  493. // 取出每一个字符
  494. char c = string.charAt(i);
  495. // 转换为unicode
  496. unicode.append("\\u").append(Integer.toHexString(c));
  497. }
  498. return unicode.toString();
  499. }
  500. /**
  501. * unicode 转字符串
  502. */
  503. public static String unicode2String(String unicode) {
  504. StringBuilder string = new StringBuilder();
  505. String[] hex = unicode.split("\\\\u");
  506. for (int i = 1; i < hex.length; i++) {
  507. // 转换出每一个代码点
  508. int data = Integer.parseInt(hex[i], 16);
  509. // 追加成string
  510. string.append((char) data);
  511. }
  512. return string.toString();
  513. }
  514. /**
  515. * 字符串转byte[]
  516. *
  517. * @param str
  518. * @param charsetName
  519. * @return
  520. */
  521. public static byte[] str2Bytes(String str, String charsetName) {
  522. if (TextUtils.isEmpty(str)) {
  523. return null;
  524. }
  525. try {
  526. return str.getBytes(charsetName);
  527. } catch (UnsupportedEncodingException e) {
  528. e.printStackTrace();
  529. return null;
  530. }
  531. }
  532. /**
  533. * 字符串转byte[],字符集默认是 utf-8
  534. *
  535. * @param str
  536. * @return
  537. */
  538. public static byte[] str2Bytes(String str) {
  539. return str2Bytes(str, "utf-8");
  540. }
  541. /**
  542. * byte[]转字符串
  543. *
  544. * @param bytes
  545. * @param charsetName
  546. * @return
  547. */
  548. public static String bytes2Str(byte[] bytes, String charsetName) {
  549. try {
  550. return new String(bytes, charsetName);
  551. } catch (UnsupportedEncodingException e) {
  552. e.printStackTrace();
  553. return null;
  554. }
  555. }
  556. /**
  557. * byte[]转字符串,字符集默认是 utf-8
  558. *
  559. * @param bytes
  560. * @return
  561. */
  562. public static String bytes2Str(byte[] bytes) {
  563. return bytes2Str(bytes, "utf-8");
  564. }
  565. }

参考

Java 中 byte、byte 数组和 int、long 之间的转换
大端小端模式
Java中16进制与字符串之间的相互转换