ByteUtil.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. package cn.com.lzt.common.util;
  2. import org.apache.commons.lang.ArrayUtils;
  3. import java.nio.charset.Charset;
  4. /**
  5. *
  6. * @Description: 字节数组转换工具类
  7. * @author fun
  8. * @date 2019年3月27日
  9. */
  10. public class ByteUtil {
  11. public static final String GBK = "GBK";
  12. public static final String UTF8 = "utf-8";
  13. public static final char[] ascii = "0123456789ABCDEF".toCharArray();
  14. private static char[] HEX_VOCABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  15. /**
  16. * 将short整型数值转换为字节数组
  17. *
  18. * @param data
  19. * @return
  20. */
  21. public static byte[] getBytes(short data) {
  22. byte[] bytes = new byte[2];
  23. bytes[0] = (byte) ((data & 0xff00) >> 8);
  24. bytes[1] = (byte) (data & 0xff);
  25. return bytes;
  26. }
  27. /**
  28. * 将字符转换为字节数组
  29. *
  30. * @param data
  31. * @return
  32. */
  33. public static byte[] getBytes(char data) {
  34. byte[] bytes = new byte[2];
  35. bytes[0] = (byte) (data >> 8);
  36. bytes[1] = (byte) (data);
  37. return bytes;
  38. }
  39. /**
  40. * 将布尔值转换为字节数组
  41. *
  42. * @param data
  43. * @return
  44. */
  45. public static byte[] getBytes(boolean data) {
  46. byte[] bytes = new byte[1];
  47. bytes[0] = (byte) (data ? 1 : 0);
  48. return bytes;
  49. }
  50. /**
  51. * 将整型数值转换为字节数组
  52. *
  53. * @param data
  54. * @return
  55. */
  56. public static byte[] getBytes(int data) {
  57. byte[] bytes = new byte[4];
  58. bytes[0] = (byte) ((data & 0xff000000) >> 24);
  59. bytes[1] = (byte) ((data & 0xff0000) >> 16);
  60. bytes[2] = (byte) ((data & 0xff00) >> 8);
  61. bytes[3] = (byte) (data & 0xff);
  62. return bytes;
  63. }
  64. /**
  65. * 将long整型数值转换为字节数组
  66. *
  67. * @param data
  68. * @return
  69. */
  70. public static byte[] getBytes(long data) {
  71. byte[] bytes = new byte[8];
  72. bytes[0] = (byte) ((data >> 56) & 0xff);
  73. bytes[1] = (byte) ((data >> 48) & 0xff);
  74. bytes[2] = (byte) ((data >> 40) & 0xff);
  75. bytes[3] = (byte) ((data >> 32) & 0xff);
  76. bytes[4] = (byte) ((data >> 24) & 0xff);
  77. bytes[5] = (byte) ((data >> 16) & 0xff);
  78. bytes[6] = (byte) ((data >> 8) & 0xff);
  79. bytes[7] = (byte) (data & 0xff);
  80. return bytes;
  81. }
  82. /**
  83. * 将float型数值转换为字节数组
  84. *
  85. * @param data
  86. * @return
  87. */
  88. public static byte[] getBytes(float data) {
  89. int intBits = Float.floatToIntBits(data);
  90. return getBytes(intBits);
  91. }
  92. /**
  93. *
  94. * @Title: getBytes
  95. * @Description: 将double型数值转换为字节数组
  96. * @return byte[]
  97. * @author fun
  98. * @date 2019年3月27日
  99. */
  100. public static byte[] getBytes(double data) {
  101. long intBits = Double.doubleToLongBits(data);
  102. return getBytes(intBits);
  103. }
  104. /**
  105. *
  106. * @Title: getBytes
  107. * @Description: 将字符串按照charsetName编码格式的字节数组
  108. * @return byte[]
  109. * @param data 字符串
  110. * @param charsetName
  111. * @author fun
  112. * @date 2019年3月27日
  113. */
  114. public static byte[] getBytes(String data, String charsetName) {
  115. Charset charset = Charset.forName(charsetName);
  116. return data.getBytes(charset);
  117. }
  118. /**
  119. *
  120. * @Title: getBytes
  121. * @Description: 将字符串按照GBK编码格式的字节数组
  122. * @return byte[]
  123. * @author fun
  124. * @date 2019年3月27日
  125. */
  126. public static byte[] getBytes(String data) {
  127. return getBytes(data, GBK);
  128. }
  129. /**
  130. *
  131. * @Title: getBoolean
  132. * @Description: 将字节数组第0字节转换为布尔值
  133. * @return boolean
  134. * @author fun
  135. * @date 2019年3月27日
  136. */
  137. public static boolean getBoolean(byte[] bytes) {
  138. return bytes[0] == 1;
  139. }
  140. /**
  141. *
  142. * @Title: getBoolean
  143. * @Description: 将字节数组的第index字节转换为布尔值
  144. * @return boolean
  145. * @author fun
  146. * @date 2019年3月27日
  147. */
  148. public static boolean getBoolean(byte[] bytes, int index) {
  149. return bytes[index] == 1;
  150. }
  151. /**
  152. *
  153. * @Title: getShort
  154. * @Description: 将字节数组前2字节转换为short整型数值
  155. * @return short
  156. * @author fun
  157. * @date 2019年3月27日
  158. */
  159. public static short getShort(byte[] bytes) {
  160. return (short) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
  161. }
  162. /**
  163. *
  164. * @Title: getShort
  165. * @Description: 将字节数组从startIndex开始的2个字节转换为short整型数值
  166. * @return short
  167. * @author fun
  168. * @date 2019年3月27日
  169. */
  170. public static short getShort(byte[] bytes, int startIndex) {
  171. return (short) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
  172. }
  173. public static short getShort_LE(byte[] bytes, int startIndex) {
  174. return (short) ((0xff00 & (bytes[startIndex + 1] << 8)) | (0xff & bytes[startIndex]));
  175. }
  176. /**
  177. *
  178. * @Title: getChar
  179. * @Description: 将字节数组前2字节转换为字符
  180. * @return char
  181. * @author fun
  182. * @date 2019年3月27日
  183. */
  184. public static char getChar(byte[] bytes) {
  185. return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
  186. }
  187. /**
  188. *
  189. * @Title: getChar
  190. * @Description: 将字节数组从startIndex开始的2个字节转换为字符
  191. * @return char
  192. * @author fun
  193. * @date 2019年3月27日
  194. */
  195. public static char getChar(byte[] bytes, int startIndex) {
  196. return (char) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
  197. }
  198. /**
  199. *
  200. * @Title: getInt
  201. * @Description: 将字节数组前4字节转换为整型数值
  202. * @return int
  203. * @author fun
  204. * @date 2019年3月27日
  205. */
  206. public static int getInt(byte[] bytes) {
  207. return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16)) | (0xff00 & (bytes[2] << 8))
  208. | (0xff & bytes[3]));
  209. }
  210. /**
  211. *
  212. * @Title: getInt
  213. * @Description: 将字节数组从startIndex开始的4个字节转换为整型数值
  214. * @return int
  215. * @author fun
  216. * @date 2019年3月27日
  217. */
  218. public static int getInt(byte[] bytes, int startIndex) {
  219. return (0xff000000 & (bytes[startIndex] << 24) | (0xff0000 & (bytes[startIndex + 1] << 16))
  220. | (0xff00 & (bytes[startIndex + 2] << 8)) | (0xff & bytes[startIndex + 3]));
  221. }
  222. /**
  223. * 将字节数组从startIndex开始的4个字节转换为整型数值 - 小端模式
  224. * @param bytes
  225. * @param startIndex
  226. * @return
  227. */
  228. public static int getInt_LE(byte[] bytes, int startIndex) {
  229. return (0xff000000 & (bytes[startIndex + 3] << 24) | (0xff0000 & (bytes[startIndex + 2] << 16))
  230. | (0xff00 & (bytes[startIndex + 1] << 8)) | (0xff & bytes[startIndex]));
  231. }
  232. /**
  233. * 将字节数组前8字节转换为long整型数值
  234. *
  235. * @param bytes
  236. * @return
  237. */
  238. public static long getLong(byte[] bytes) {
  239. return (0xff00000000000000L & ((long) bytes[0] << 56) | (0xff000000000000L & ((long) bytes[1] << 48))
  240. | (0xff0000000000L & ((long) bytes[2] << 40)) | (0xff00000000L & ((long) bytes[3] << 32))
  241. | (0xff000000L & ((long) bytes[4] << 24)) | (0xff0000L & ((long) bytes[5] << 16))
  242. | (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));
  243. }
  244. /**
  245. * 将字节数组从startIndex开始的8个字节转换为long整型数值
  246. *
  247. * @param bytes
  248. * @param startIndex
  249. * @return
  250. */
  251. public static long getLong(byte[] bytes, int startIndex) {
  252. return (0xff00000000000000L & ((long) bytes[startIndex] << 56)
  253. | (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))
  254. | (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))
  255. | (0xff00000000L & ((long) bytes[startIndex + 3] << 32))
  256. | (0xff000000L & ((long) bytes[startIndex + 4] << 24))
  257. | (0xff0000L & ((long) bytes[startIndex + 5] << 16)) | (0xff00L & ((long) bytes[startIndex + 6] << 8))
  258. | (0xffL & (long) bytes[startIndex + 7]));
  259. }
  260. /**
  261. * 将字节数组前4字节转换为float型数值
  262. *
  263. * @param bytes
  264. * @return
  265. */
  266. public static float getFloat(byte[] bytes) {
  267. return Float.intBitsToFloat(getInt(bytes));
  268. }
  269. /**
  270. * 将字节数组从startIndex开始的4个字节转换为float型数值
  271. *
  272. * @param bytes
  273. * @param startIndex
  274. * @return
  275. */
  276. public static float getFloat(byte[] bytes, int startIndex) {
  277. byte[] result = new byte[4];
  278. System.arraycopy(bytes, startIndex, result, 0, 4);
  279. return Float.intBitsToFloat(getInt(result));
  280. }
  281. /**
  282. * 将字节数组前8字节转换为double型数值
  283. *
  284. * @param bytes
  285. * @return
  286. */
  287. public static double getDouble(byte[] bytes) {
  288. long l = getLong(bytes);
  289. return Double.longBitsToDouble(l);
  290. }
  291. /**
  292. * 将字节数组从startIndex开始的8个字节转换为double型数值
  293. *
  294. * @param bytes
  295. * @param startIndex
  296. * @return
  297. */
  298. public static double getDouble(byte[] bytes, int startIndex) {
  299. byte[] result = new byte[8];
  300. System.arraycopy(bytes, startIndex, result, 0, 8);
  301. long l = getLong(result);
  302. return Double.longBitsToDouble(l);
  303. }
  304. /**
  305. * 将charsetName编码格式的字节数组转换为字符串
  306. *
  307. * @param bytes
  308. * @param charsetName
  309. * @return
  310. */
  311. public static String getString(byte[] bytes, String charsetName) {
  312. return new String(bytes, Charset.forName(charsetName));
  313. }
  314. /**
  315. * 将GBK编码格式的字节数组转换为字符串
  316. *
  317. * @param bytes
  318. * @return
  319. */
  320. public static String getString(byte[] bytes) {
  321. return getString(bytes, GBK);
  322. }
  323. /**
  324. * 将16进制字符串转换为字节数组
  325. *
  326. * @param hex
  327. * @return
  328. */
  329. public static byte[] hexStringToBytes(String hex) {
  330. if (hex == null || "".equals(hex)) {
  331. return null;
  332. }
  333. int len = hex.length() / 2;
  334. byte[] result = new byte[len];
  335. char[] chArr = hex.toCharArray();
  336. for (int i = 0; i < len; i++) {
  337. int pos = i * 2;
  338. result[i] = (byte) (toByte(chArr[pos]) << 4 | toByte(chArr[pos + 1]));
  339. }
  340. return result;
  341. }
  342. /**
  343. * 将16进制字符串转换为字节数组
  344. *
  345. * @param hex
  346. * @return
  347. */
  348. public static byte[] hexToBytes(String hex) {
  349. if (hex.length() % 2 != 0)
  350. throw new IllegalArgumentException("input string should be any multiple of 2!");
  351. hex.toUpperCase();
  352. byte[] byteBuffer = new byte[hex.length() / 2];
  353. byte padding = 0x00;
  354. boolean paddingTurning = false;
  355. for (int i = 0; i < hex.length(); i++) {
  356. if (paddingTurning) {
  357. char c = hex.charAt(i);
  358. int index = indexOf(hex, c);
  359. padding = (byte) ((padding << 4) | index);
  360. byteBuffer[i / 2] = padding;
  361. padding = 0x00;
  362. paddingTurning = false;
  363. } else {
  364. char c = hex.charAt(i);
  365. int index = indexOf(hex, c);
  366. padding = (byte) (padding | index);
  367. paddingTurning = true;
  368. }
  369. }
  370. return byteBuffer;
  371. }
  372. private static int indexOf(String input, char c) {
  373. int index = ArrayUtils.indexOf(HEX_VOCABLE, c);
  374. if (index < 0) {
  375. throw new IllegalArgumentException("err input:" + input);
  376. }
  377. return index;
  378. }
  379. /**
  380. * 将BCD编码的字节数组转换为字符串
  381. *
  382. * @param bcds
  383. * @return
  384. */
  385. public static String bcdToString(byte[] bcds) {
  386. if (bcds == null || bcds.length == 0) {
  387. return null;
  388. }
  389. byte[] temp = new byte[2 * bcds.length];
  390. for (int i = 0; i < bcds.length; i++) {
  391. temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);
  392. temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);
  393. }
  394. StringBuffer res = new StringBuffer();
  395. for (int i = 0; i < temp.length; i++) {
  396. res.append(ascii[temp[i]]);
  397. }
  398. return res.toString();
  399. }
  400. /**
  401. * 字节转整形
  402. *
  403. * @param value
  404. * @return
  405. */
  406. public static int bcdToInt(byte value) {
  407. return ((value >> 4) * 10) + (value & 0x0F);
  408. }
  409. /**
  410. * 字节数组转16进制字符串
  411. *
  412. * @param bs
  413. * @return
  414. */
  415. public static String bytesToHex(byte[] bs) {
  416. StringBuilder sb = new StringBuilder();
  417. for (byte b : bs) {
  418. int high = (b >> 4) & 0x0f;
  419. int low = b & 0x0f;
  420. sb.append(HEX_VOCABLE[high]);
  421. sb.append(HEX_VOCABLE[low]);
  422. }
  423. return sb.toString();
  424. }
  425. /**
  426. * 字节数组取前len个字节转16进制字符串
  427. *
  428. * @param bs
  429. * @param len
  430. * @return
  431. */
  432. public static String bytesToHex(byte[] bs, int len) {
  433. StringBuilder sb = new StringBuilder();
  434. for (int i = 0; i < len; i++) {
  435. byte b = bs[i];
  436. int high = (b >> 4) & 0x0f;
  437. int low = b & 0x0f;
  438. sb.append(HEX_VOCABLE[high]);
  439. sb.append(HEX_VOCABLE[low]);
  440. }
  441. return sb.toString();
  442. }
  443. /**
  444. * 字节数组偏移offset长度之后的取len个字节转16进制字符串
  445. *
  446. * @param bs
  447. * @param offset
  448. * @param len
  449. * @return
  450. */
  451. public static String bytesToHex(byte[] bs, int offset, int len) {
  452. StringBuilder sb = new StringBuilder();
  453. for (int i = 0; i < len; i++) {
  454. byte b = bs[offset + i];
  455. int high = (b >> 4) & 0x0f;
  456. int low = b & 0x0f;
  457. sb.append(HEX_VOCABLE[high]);
  458. sb.append(HEX_VOCABLE[low]);
  459. }
  460. return sb.toString();
  461. }
  462. /**
  463. * 字节转16进制字符串
  464. *
  465. * @param b
  466. * @return
  467. */
  468. public static String byteToHex(byte b) {
  469. StringBuilder sb = new StringBuilder();
  470. int high = (b >> 4) & 0x0f;
  471. int low = b & 0x0f;
  472. sb.append(HEX_VOCABLE[high]);
  473. sb.append(HEX_VOCABLE[low]);
  474. String s = sb.toString();
  475. if ("".equals(s.substring(0, 1))) {
  476. return s.substring(1);
  477. } else {
  478. return s;
  479. }
  480. }
  481. /**
  482. * 将字节数组取反
  483. *
  484. * @param src
  485. * @return
  486. */
  487. public static String negate(byte[] src) {
  488. if (src == null || src.length == 0) {
  489. return null;
  490. }
  491. byte[] temp = new byte[2 * src.length];
  492. for (int i = 0; i < src.length; i++) {
  493. byte tmp = (byte) (0xFF ^ src[i]);
  494. temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
  495. temp[i * 2 + 1] = (byte) (tmp & 0x0f);
  496. }
  497. StringBuffer res = new StringBuffer();
  498. for (int i = 0; i < temp.length; i++) {
  499. res.append(ascii[temp[i]]);
  500. }
  501. return res.toString();
  502. }
  503. /**
  504. * 比较字节数组是否相同
  505. *
  506. * @param a
  507. * @param b
  508. * @return
  509. */
  510. public static boolean compareBytes(byte[] a, byte[] b) {
  511. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
  512. return false;
  513. }
  514. if (a.length == b.length) {
  515. for (int i = 0; i < a.length; i++) {
  516. if (a[i] != b[i]) {
  517. return false;
  518. }
  519. }
  520. } else {
  521. return false;
  522. }
  523. return true;
  524. }
  525. /**
  526. * 只比对指定长度byte
  527. *
  528. * @param a
  529. * @param b
  530. * @param len
  531. * @return
  532. */
  533. public static boolean compareBytes(byte[] a, byte[] b, int len) {
  534. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length < len || b.length < len) {
  535. return false;
  536. }
  537. for (int i = 0; i < len; i++) {
  538. if (a[i] != b[i]) {
  539. return false;
  540. }
  541. }
  542. return true;
  543. }
  544. /**
  545. * 将字节数组转换为二进制字符串
  546. *
  547. * @param items
  548. * @return
  549. */
  550. public static String bytesToBinaryString(byte[] items) {
  551. if (items == null || items.length == 0) {
  552. return null;
  553. }
  554. StringBuffer buf = new StringBuffer();
  555. for (byte item : items) {
  556. buf.append(byteToBinaryString(item));
  557. }
  558. return buf.toString();
  559. }
  560. /**
  561. * 将字节转换为二进制字符串
  562. *
  563. * @param item
  564. * @return
  565. */
  566. public static String byteToBinaryString(byte item) {
  567. byte a = item;
  568. StringBuffer buf = new StringBuffer();
  569. for (int i = 0; i < 8; i++) {
  570. buf.insert(0, a % 2);
  571. a = (byte) (a >> 1);
  572. }
  573. return buf.toString();
  574. }
  575. /**
  576. * 对数组a,b进行异或运算
  577. *
  578. * @param a
  579. * @param b
  580. * @return
  581. */
  582. public static byte[] xor(byte[] a, byte[] b) {
  583. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
  584. return null;
  585. }
  586. byte[] result = new byte[a.length];
  587. for (int i = 0; i < a.length; i++) {
  588. result[i] = (byte) (a[i] ^ b[i]);
  589. }
  590. return result;
  591. }
  592. /**
  593. * 对数组a,b进行异或运算 运算长度len
  594. *
  595. * @param a
  596. * @param b
  597. * @param len
  598. * @return
  599. */
  600. public static byte[] xor(byte[] a, byte[] b, int len) {
  601. if (a == null || a.length == 0 || b == null || b.length == 0) {
  602. return null;
  603. }
  604. if (a.length < len || b.length < len) {
  605. return null;
  606. }
  607. byte[] result = new byte[len];
  608. for (int i = 0; i < len; i++) {
  609. result[i] = (byte) (a[i] ^ b[i]);
  610. }
  611. return result;
  612. }
  613. /**
  614. * 将short整型数值转换为字节数组
  615. *
  616. * @param num
  617. * @return
  618. */
  619. public static byte[] shortToBytes(int num) {
  620. byte[] temp = new byte[2];
  621. for (int i = 0; i < 2; i++) {
  622. temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
  623. }
  624. return temp;
  625. }
  626. /**
  627. * 将字节数组转为整型
  628. *
  629. * @param arr
  630. * @return
  631. */
  632. public static int bytesToShort(byte[] arr) {
  633. int mask = 0xFF;
  634. int temp = 0;
  635. int result = 0;
  636. for (int i = 0; i < 2; i++) {
  637. result <<= 8;
  638. temp = arr[i] & mask;
  639. result |= temp;
  640. }
  641. return result;
  642. }
  643. /**
  644. * 将整型数值转换为指定长度的字节数组
  645. *
  646. * @param num
  647. * @return
  648. */
  649. public static byte[] intToBytes(int num) {
  650. byte[] temp = new byte[4];
  651. for (int i = 0; i < 4; i++) {
  652. temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
  653. }
  654. return temp;
  655. }
  656. /**
  657. * 将整型数值转换为指定长度的字节数组
  658. *
  659. * @param src
  660. * @param len
  661. * @return
  662. */
  663. public static byte[] intToBytes(int src, int len) {
  664. if (len < 1 || len > 4) {
  665. return null;
  666. }
  667. byte[] temp = new byte[len];
  668. for (int i = 0; i < len; i++) {
  669. temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
  670. }
  671. return temp;
  672. }
  673. /**
  674. * 将字节数组转换为整型数值
  675. *
  676. * @param arr
  677. * @return
  678. */
  679. public static int bytesToInt(byte[] arr) {
  680. int mask = 0xFF;
  681. int temp = 0;
  682. int result = 0;
  683. for (int i = 0; i < 4; i++) {
  684. result <<= 8;
  685. temp = arr[i] & mask;
  686. result |= temp;
  687. }
  688. return result;
  689. }
  690. /**
  691. * 将long整型数值转换为字节数组
  692. *
  693. * @param num
  694. * @return
  695. */
  696. public static byte[] longToBytes(long num) {
  697. byte[] temp = new byte[8];
  698. for (int i = 0; i < 8; i++) {
  699. temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
  700. }
  701. return temp;
  702. }
  703. /**
  704. * 将字节数组转换为long整型数值
  705. *
  706. * @param arr
  707. * @return
  708. */
  709. public static long bytesToLong(byte[] arr) {
  710. int mask = 0xFF;
  711. int temp = 0;
  712. long result = 0;
  713. int len = Math.min(8, arr.length);
  714. for (int i = 0; i < len; i++) {
  715. result <<= 8;
  716. temp = arr[i] & mask;
  717. result |= temp;
  718. }
  719. return result;
  720. }
  721. /**
  722. * 将16进制字符转换为字节
  723. *
  724. * @param c
  725. * @return
  726. */
  727. public static byte toByte(char c) {
  728. byte b = (byte) "0123456789ABCDEF".indexOf(c);
  729. return b;
  730. }
  731. /**
  732. * 功能描述:把两个字节的字节数组转化为整型数据,高位补零,例如:<br/>
  733. * 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:<br/>
  734. * 00000000 00000000 00000001 00000010,函数返回258
  735. *
  736. * @param lenData
  737. * 需要进行转换的字节数组
  738. * @return 字节数组所表示整型值的大小
  739. */
  740. public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
  741. if (lenData.length != 2) {
  742. return -1;
  743. }
  744. byte fill[] = new byte[] { 0, 0 };
  745. byte real[] = new byte[4];
  746. System.arraycopy(fill, 0, real, 0, 2);
  747. System.arraycopy(lenData, 0, real, 2, 2);
  748. int len = byteToInt(real);
  749. return len;
  750. }
  751. /**
  752. * 功能描述:将byte数组转化为int类型的数据
  753. *
  754. * @param byteVal
  755. * 需要转化的字节数组
  756. * @return 字节数组所表示的整型数据
  757. */
  758. public static int byteToInt(byte[] byteVal) {
  759. int result = 0;
  760. for (int i = 0; i < byteVal.length; i++) {
  761. int tmpVal = (byteVal[i] << (8 * (3 - i)));
  762. switch (i) {
  763. case 0:
  764. tmpVal = tmpVal & 0xFF000000;
  765. break;
  766. case 1:
  767. tmpVal = tmpVal & 0x00FF0000;
  768. break;
  769. case 2:
  770. tmpVal = tmpVal & 0x0000FF00;
  771. break;
  772. case 3:
  773. tmpVal = tmpVal & 0x000000FF;
  774. break;
  775. }
  776. result = result | tmpVal;
  777. }
  778. return result;
  779. }
  780. public static byte CheckXORSum(byte[] bData) {
  781. byte sum = 0x00;
  782. for (int i = 0; i < bData.length; i++) {
  783. sum ^= bData[i];
  784. }
  785. return sum;
  786. }
  787. /**
  788. * 从offset开始 将后续长度为len的byte字节转为int
  789. *
  790. * @param data
  791. * @param offset
  792. * @param len
  793. * @return
  794. */
  795. public static int bytesToInt(byte[] data, int offset, int len) {
  796. int mask = 0xFF;
  797. int temp = 0;
  798. int result = 0;
  799. len = Math.min(len, 4);
  800. for (int i = 0; i < len; i++) {
  801. result <<= 8;
  802. temp = data[offset + i] & mask;
  803. result |= temp;
  804. }
  805. return result;
  806. }
  807. /**
  808. * byte字节数组中的字符串的长度
  809. *
  810. * @param data
  811. * @return
  812. */
  813. public static int getBytesStringLen(byte[] data) {
  814. int count = 0;
  815. for (byte b : data) {
  816. if (b == 0x00)
  817. break;
  818. count++;
  819. }
  820. return count;
  821. }
  822. /**
  823. *
  824. * @Title: hexString2binaryString
  825. * @Description: 十六进制字符串转二进制字符串
  826. * @return String
  827. * @author fun
  828. * @date 2019年3月27日
  829. */
  830. public static String hexString2binaryString(String hexString) {
  831. if (hexString == null || hexString.length() % 2 != 0)
  832. return null;
  833. String bString = "", tmp;
  834. for (int i = 0; i < hexString.length(); i++) {
  835. tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
  836. bString += tmp.substring(tmp.length() - 4);
  837. }
  838. return bString;
  839. }
  840. /**
  841. * int转换为小端byte[](高位放在高地址中)
  842. * @param iValue
  843. * @return
  844. */
  845. public byte[] Int2Bytes_LE(int iValue){
  846. byte[] rst = new byte[4];
  847. // 先写int的最后一个字节
  848. rst[0] = (byte)(iValue & 0xFF);
  849. // int 倒数第二个字节
  850. rst[1] = (byte)((iValue & 0xFF00) >> 8 );
  851. // int 倒数第三个字节
  852. rst[2] = (byte)((iValue & 0xFF0000) >> 16 );
  853. // int 第一个字节
  854. rst[3] = (byte)((iValue & 0xFF000000) >> 24 );
  855. return rst;
  856. }
  857. }