BLE_WIFI_Scale_Server_Api 服务器与wifi秤交互只需要实现3个接口:设备注册、获取用户、上传记录
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BytesUtils.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package com.inet.ailink.receiver.common.utils;
  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 BytesUtils {
  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. /**
  174. *
  175. * @Title: getChar
  176. * @Description: 将字节数组前2字节转换为字符
  177. * @return char
  178. * @author fun
  179. * @date 2019年3月27日
  180. */
  181. public static char getChar(byte[] bytes) {
  182. return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
  183. }
  184. /**
  185. *
  186. * @Title: getChar
  187. * @Description: 将字节数组从startIndex开始的2个字节转换为字符
  188. * @return char
  189. * @author fun
  190. * @date 2019年3月27日
  191. */
  192. public static char getChar(byte[] bytes, int startIndex) {
  193. return (char) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
  194. }
  195. /**
  196. *
  197. * @Title: getInt
  198. * @Description: 将字节数组前4字节转换为整型数值
  199. * @return int
  200. * @author fun
  201. * @date 2019年3月27日
  202. */
  203. public static int getInt(byte[] bytes) {
  204. return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16)) | (0xff00 & (bytes[2] << 8))
  205. | (0xff & bytes[3]));
  206. }
  207. /**
  208. *
  209. * @Title: getInt
  210. * @Description: 将字节数组从startIndex开始的4个字节转换为整型数值
  211. * @return int
  212. * @author fun
  213. * @date 2019年3月27日
  214. */
  215. public static int getInt(byte[] bytes, int startIndex) {
  216. return (0xff000000 & (bytes[startIndex] << 24) | (0xff0000 & (bytes[startIndex + 1] << 16))
  217. | (0xff00 & (bytes[startIndex + 2] << 8)) | (0xff & bytes[startIndex + 3]));
  218. }
  219. /**
  220. * 将字节数组前8字节转换为long整型数值
  221. *
  222. * @param bytes
  223. * @return
  224. */
  225. public static long getLong(byte[] bytes) {
  226. return (0xff00000000000000L & ((long) bytes[0] << 56) | (0xff000000000000L & ((long) bytes[1] << 48))
  227. | (0xff0000000000L & ((long) bytes[2] << 40)) | (0xff00000000L & ((long) bytes[3] << 32))
  228. | (0xff000000L & ((long) bytes[4] << 24)) | (0xff0000L & ((long) bytes[5] << 16))
  229. | (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));
  230. }
  231. /**
  232. * 将字节数组从startIndex开始的8个字节转换为long整型数值
  233. *
  234. * @param bytes
  235. * @param startIndex
  236. * @return
  237. */
  238. public static long getLong(byte[] bytes, int startIndex) {
  239. return (0xff00000000000000L & ((long) bytes[startIndex] << 56)
  240. | (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))
  241. | (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))
  242. | (0xff00000000L & ((long) bytes[startIndex + 3] << 32))
  243. | (0xff000000L & ((long) bytes[startIndex + 4] << 24))
  244. | (0xff0000L & ((long) bytes[startIndex + 5] << 16)) | (0xff00L & ((long) bytes[startIndex + 6] << 8))
  245. | (0xffL & (long) bytes[startIndex + 7]));
  246. }
  247. /**
  248. * 将字节数组前4字节转换为float型数值
  249. *
  250. * @param bytes
  251. * @return
  252. */
  253. public static float getFloat(byte[] bytes) {
  254. return Float.intBitsToFloat(getInt(bytes));
  255. }
  256. /**
  257. * 将字节数组从startIndex开始的4个字节转换为float型数值
  258. *
  259. * @param bytes
  260. * @param startIndex
  261. * @return
  262. */
  263. public static float getFloat(byte[] bytes, int startIndex) {
  264. byte[] result = new byte[4];
  265. System.arraycopy(bytes, startIndex, result, 0, 4);
  266. return Float.intBitsToFloat(getInt(result));
  267. }
  268. /**
  269. * 将字节数组前8字节转换为double型数值
  270. *
  271. * @param bytes
  272. * @return
  273. */
  274. public static double getDouble(byte[] bytes) {
  275. long l = getLong(bytes);
  276. return Double.longBitsToDouble(l);
  277. }
  278. /**
  279. * 将字节数组从startIndex开始的8个字节转换为double型数值
  280. *
  281. * @param bytes
  282. * @param startIndex
  283. * @return
  284. */
  285. public static double getDouble(byte[] bytes, int startIndex) {
  286. byte[] result = new byte[8];
  287. System.arraycopy(bytes, startIndex, result, 0, 8);
  288. long l = getLong(result);
  289. return Double.longBitsToDouble(l);
  290. }
  291. /**
  292. * 将charsetName编码格式的字节数组转换为字符串
  293. *
  294. * @param bytes
  295. * @param charsetName
  296. * @return
  297. */
  298. public static String getString(byte[] bytes, String charsetName) {
  299. return new String(bytes, Charset.forName(charsetName));
  300. }
  301. /**
  302. * 将GBK编码格式的字节数组转换为字符串
  303. *
  304. * @param bytes
  305. * @return
  306. */
  307. public static String getString(byte[] bytes) {
  308. return getString(bytes, GBK);
  309. }
  310. /**
  311. * 将16进制字符串转换为字节数组
  312. *
  313. * @param hex
  314. * @return
  315. */
  316. public static byte[] hexStringToBytes(String hex) {
  317. if (hex == null || "".equals(hex)) {
  318. return null;
  319. }
  320. int len = hex.length() / 2;
  321. byte[] result = new byte[len];
  322. char[] chArr = hex.toCharArray();
  323. for (int i = 0; i < len; i++) {
  324. int pos = i * 2;
  325. result[i] = (byte) (toByte(chArr[pos]) << 4 | toByte(chArr[pos + 1]));
  326. }
  327. return result;
  328. }
  329. /**
  330. * 将16进制字符串转换为字节数组
  331. *
  332. * @param hex
  333. * @return
  334. */
  335. public static byte[] hexToBytes(String hex) {
  336. if (hex.length() % 2 != 0)
  337. throw new IllegalArgumentException("input string should be any multiple of 2!");
  338. hex.toUpperCase();
  339. byte[] byteBuffer = new byte[hex.length() / 2];
  340. byte padding = 0x00;
  341. boolean paddingTurning = false;
  342. for (int i = 0; i < hex.length(); i++) {
  343. if (paddingTurning) {
  344. char c = hex.charAt(i);
  345. int index = indexOf(hex, c);
  346. padding = (byte) ((padding << 4) | index);
  347. byteBuffer[i / 2] = padding;
  348. padding = 0x00;
  349. paddingTurning = false;
  350. } else {
  351. char c = hex.charAt(i);
  352. int index = indexOf(hex, c);
  353. padding = (byte) (padding | index);
  354. paddingTurning = true;
  355. }
  356. }
  357. return byteBuffer;
  358. }
  359. private static int indexOf(String input, char c) {
  360. int index = ArrayUtils.indexOf(HEX_VOCABLE, c);
  361. if (index < 0) {
  362. throw new IllegalArgumentException("err input:" + input);
  363. }
  364. return index;
  365. }
  366. /**
  367. * 将BCD编码的字节数组转换为字符串
  368. *
  369. * @param bcds
  370. * @return
  371. */
  372. public static String bcdToString(byte[] bcds) {
  373. if (bcds == null || bcds.length == 0) {
  374. return null;
  375. }
  376. byte[] temp = new byte[2 * bcds.length];
  377. for (int i = 0; i < bcds.length; i++) {
  378. temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);
  379. temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);
  380. }
  381. StringBuffer res = new StringBuffer();
  382. for (int i = 0; i < temp.length; i++) {
  383. res.append(ascii[temp[i]]);
  384. }
  385. return res.toString();
  386. }
  387. /**
  388. * 字节转整形
  389. *
  390. * @param value
  391. * @return
  392. */
  393. public static int bcdToInt(byte value) {
  394. return ((value >> 4) * 10) + (value & 0x0F);
  395. }
  396. /**
  397. * 字节数组转16进制字符串
  398. *
  399. * @param bs
  400. * @return
  401. */
  402. public static String bytesToHex(byte[] bs) {
  403. StringBuilder sb = new StringBuilder();
  404. for (byte b : bs) {
  405. int high = (b >> 4) & 0x0f;
  406. int low = b & 0x0f;
  407. sb.append(HEX_VOCABLE[high]);
  408. sb.append(HEX_VOCABLE[low]);
  409. }
  410. return sb.toString();
  411. }
  412. /**
  413. * 字节数组取前len个字节转16进制字符串
  414. *
  415. * @param bs
  416. * @param len
  417. * @return
  418. */
  419. public static String bytesToHex(byte[] bs, int len) {
  420. StringBuilder sb = new StringBuilder();
  421. for (int i = 0; i < len; i++) {
  422. byte b = bs[i];
  423. int high = (b >> 4) & 0x0f;
  424. int low = b & 0x0f;
  425. sb.append(HEX_VOCABLE[high]);
  426. sb.append(HEX_VOCABLE[low]);
  427. }
  428. return sb.toString();
  429. }
  430. /**
  431. * 字节数组偏移offset长度之后的取len个字节转16进制字符串
  432. *
  433. * @param bs
  434. * @param offset
  435. * @param len
  436. * @return
  437. */
  438. public static String bytesToHex(byte[] bs, int offset, int len) {
  439. StringBuilder sb = new StringBuilder();
  440. for (int i = 0; i < len; i++) {
  441. byte b = bs[offset + i];
  442. int high = (b >> 4) & 0x0f;
  443. int low = b & 0x0f;
  444. sb.append(HEX_VOCABLE[high]);
  445. sb.append(HEX_VOCABLE[low]);
  446. }
  447. return sb.toString();
  448. }
  449. /**
  450. * 字节转16进制字符串
  451. *
  452. * @param bs
  453. * @return
  454. */
  455. public static String byteToHex(byte b) {
  456. StringBuilder sb = new StringBuilder();
  457. int high = (b >> 4) & 0x0f;
  458. int low = b & 0x0f;
  459. sb.append(HEX_VOCABLE[high]);
  460. sb.append(HEX_VOCABLE[low]);
  461. String s = sb.toString();
  462. if ("".equals(s.substring(0, 1))) {
  463. return s.substring(1);
  464. } else {
  465. return s;
  466. }
  467. }
  468. /**
  469. * 将字节数组取反
  470. *
  471. * @param src
  472. * @return
  473. */
  474. public static String negate(byte[] src) {
  475. if (src == null || src.length == 0) {
  476. return null;
  477. }
  478. byte[] temp = new byte[2 * src.length];
  479. for (int i = 0; i < src.length; i++) {
  480. byte tmp = (byte) (0xFF ^ src[i]);
  481. temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
  482. temp[i * 2 + 1] = (byte) (tmp & 0x0f);
  483. }
  484. StringBuffer res = new StringBuffer();
  485. for (int i = 0; i < temp.length; i++) {
  486. res.append(ascii[temp[i]]);
  487. }
  488. return res.toString();
  489. }
  490. /**
  491. * 比较字节数组是否相同
  492. *
  493. * @param a
  494. * @param b
  495. * @return
  496. */
  497. public static boolean compareBytes(byte[] a, byte[] b) {
  498. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
  499. return false;
  500. }
  501. if (a.length == b.length) {
  502. for (int i = 0; i < a.length; i++) {
  503. if (a[i] != b[i]) {
  504. return false;
  505. }
  506. }
  507. } else {
  508. return false;
  509. }
  510. return true;
  511. }
  512. /**
  513. * 只比对指定长度byte
  514. *
  515. * @param a
  516. * @param b
  517. * @param len
  518. * @return
  519. */
  520. public static boolean compareBytes(byte[] a, byte[] b, int len) {
  521. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length < len || b.length < len) {
  522. return false;
  523. }
  524. for (int i = 0; i < len; i++) {
  525. if (a[i] != b[i]) {
  526. return false;
  527. }
  528. }
  529. return true;
  530. }
  531. /**
  532. * 将字节数组转换为二进制字符串
  533. *
  534. * @param items
  535. * @return
  536. */
  537. public static String bytesToBinaryString(byte[] items) {
  538. if (items == null || items.length == 0) {
  539. return null;
  540. }
  541. StringBuffer buf = new StringBuffer();
  542. for (byte item : items) {
  543. buf.append(byteToBinaryString(item));
  544. }
  545. return buf.toString();
  546. }
  547. /**
  548. * 将字节转换为二进制字符串
  549. *
  550. * @param items
  551. * @return
  552. */
  553. public static String byteToBinaryString(byte item) {
  554. byte a = item;
  555. StringBuffer buf = new StringBuffer();
  556. for (int i = 0; i < 8; i++) {
  557. buf.insert(0, a % 2);
  558. a = (byte) (a >> 1);
  559. }
  560. return buf.toString();
  561. }
  562. /**
  563. * 对数组a,b进行异或运算
  564. *
  565. * @param a
  566. * @param b
  567. * @return
  568. */
  569. public static byte[] xor(byte[] a, byte[] b) {
  570. if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
  571. return null;
  572. }
  573. byte[] result = new byte[a.length];
  574. for (int i = 0; i < a.length; i++) {
  575. result[i] = (byte) (a[i] ^ b[i]);
  576. }
  577. return result;
  578. }
  579. /**
  580. * 对数组a,b进行异或运算 运算长度len
  581. *
  582. * @param a
  583. * @param b
  584. * @param len
  585. * @return
  586. */
  587. public static byte[] xor(byte[] a, byte[] b, int len) {
  588. if (a == null || a.length == 0 || b == null || b.length == 0) {
  589. return null;
  590. }
  591. if (a.length < len || b.length < len) {
  592. return null;
  593. }
  594. byte[] result = new byte[len];
  595. for (int i = 0; i < len; i++) {
  596. result[i] = (byte) (a[i] ^ b[i]);
  597. }
  598. return result;
  599. }
  600. /**
  601. * 将short整型数值转换为字节数组
  602. *
  603. * @param num
  604. * @return
  605. */
  606. public static byte[] shortToBytes(int num) {
  607. byte[] temp = new byte[2];
  608. for (int i = 0; i < 2; i++) {
  609. temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
  610. }
  611. return temp;
  612. }
  613. /**
  614. * 将字节数组转为整型
  615. *
  616. * @param num
  617. * @return
  618. */
  619. public static int bytesToShort(byte[] arr) {
  620. int mask = 0xFF;
  621. int temp = 0;
  622. int result = 0;
  623. for (int i = 0; i < 2; i++) {
  624. result <<= 8;
  625. temp = arr[i] & mask;
  626. result |= temp;
  627. }
  628. return result;
  629. }
  630. /**
  631. * 将整型数值转换为指定长度的字节数组
  632. *
  633. * @param num
  634. * @return
  635. */
  636. public static byte[] intToBytes(int num) {
  637. byte[] temp = new byte[4];
  638. for (int i = 0; i < 4; i++) {
  639. temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
  640. }
  641. return temp;
  642. }
  643. /**
  644. * 将整型数值转换为指定长度的字节数组
  645. *
  646. * @param src
  647. * @param len
  648. * @return
  649. */
  650. public static byte[] intToBytes(int src, int len) {
  651. if (len < 1 || len > 4) {
  652. return null;
  653. }
  654. byte[] temp = new byte[len];
  655. for (int i = 0; i < len; i++) {
  656. temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
  657. }
  658. return temp;
  659. }
  660. /**
  661. * 将字节数组转换为整型数值
  662. *
  663. * @param arr
  664. * @return
  665. */
  666. public static int bytesToInt(byte[] arr) {
  667. int mask = 0xFF;
  668. int temp = 0;
  669. int result = 0;
  670. for (int i = 0; i < 4; i++) {
  671. result <<= 8;
  672. temp = arr[i] & mask;
  673. result |= temp;
  674. }
  675. return result;
  676. }
  677. /**
  678. * 将long整型数值转换为字节数组
  679. *
  680. * @param num
  681. * @return
  682. */
  683. public static byte[] longToBytes(long num) {
  684. byte[] temp = new byte[8];
  685. for (int i = 0; i < 8; i++) {
  686. temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
  687. }
  688. return temp;
  689. }
  690. /**
  691. * 将字节数组转换为long整型数值
  692. *
  693. * @param arr
  694. * @return
  695. */
  696. public static long bytesToLong(byte[] arr) {
  697. int mask = 0xFF;
  698. int temp = 0;
  699. long result = 0;
  700. int len = Math.min(8, arr.length);
  701. for (int i = 0; i < len; i++) {
  702. result <<= 8;
  703. temp = arr[i] & mask;
  704. result |= temp;
  705. }
  706. return result;
  707. }
  708. /**
  709. * 将16进制字符转换为字节
  710. *
  711. * @param c
  712. * @return
  713. */
  714. public static byte toByte(char c) {
  715. byte b = (byte) "0123456789ABCDEF".indexOf(c);
  716. return b;
  717. }
  718. /**
  719. * 功能描述:把两个字节的字节数组转化为整型数据,高位补零,例如:<br/>
  720. * 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:<br/>
  721. * 00000000 00000000 00000001 00000010,函数返回258
  722. *
  723. * @param lenData
  724. * 需要进行转换的字节数组
  725. * @return 字节数组所表示整型值的大小
  726. */
  727. public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
  728. if (lenData.length != 2) {
  729. return -1;
  730. }
  731. byte fill[] = new byte[] { 0, 0 };
  732. byte real[] = new byte[4];
  733. System.arraycopy(fill, 0, real, 0, 2);
  734. System.arraycopy(lenData, 0, real, 2, 2);
  735. int len = byteToInt(real);
  736. return len;
  737. }
  738. /**
  739. * 功能描述:将byte数组转化为int类型的数据
  740. *
  741. * @param byteVal
  742. * 需要转化的字节数组
  743. * @return 字节数组所表示的整型数据
  744. */
  745. public static int byteToInt(byte[] byteVal) {
  746. int result = 0;
  747. for (int i = 0; i < byteVal.length; i++) {
  748. int tmpVal = (byteVal[i] << (8 * (3 - i)));
  749. switch (i) {
  750. case 0:
  751. tmpVal = tmpVal & 0xFF000000;
  752. break;
  753. case 1:
  754. tmpVal = tmpVal & 0x00FF0000;
  755. break;
  756. case 2:
  757. tmpVal = tmpVal & 0x0000FF00;
  758. break;
  759. case 3:
  760. tmpVal = tmpVal & 0x000000FF;
  761. break;
  762. }
  763. result = result | tmpVal;
  764. }
  765. return result;
  766. }
  767. public static byte CheckXORSum(byte[] bData) {
  768. byte sum = 0x00;
  769. for (int i = 0; i < bData.length; i++) {
  770. sum ^= bData[i];
  771. }
  772. return sum;
  773. }
  774. /**
  775. * 从offset开始 将后续长度为len的byte字节转为int
  776. *
  777. * @param data
  778. * @param offset
  779. * @param len
  780. * @return
  781. */
  782. public static int bytesToInt(byte[] data, int offset, int len) {
  783. int mask = 0xFF;
  784. int temp = 0;
  785. int result = 0;
  786. len = Math.min(len, 4);
  787. for (int i = 0; i < len; i++) {
  788. result <<= 8;
  789. temp = data[offset + i] & mask;
  790. result |= temp;
  791. }
  792. return result;
  793. }
  794. /**
  795. * byte字节数组中的字符串的长度
  796. *
  797. * @param data
  798. * @return
  799. */
  800. public static int getBytesStringLen(byte[] data) {
  801. int count = 0;
  802. for (byte b : data) {
  803. if (b == 0x00)
  804. break;
  805. count++;
  806. }
  807. return count;
  808. }
  809. /**
  810. *
  811. * @Title: hexString2binaryString
  812. * @Description: 十六进制字符串转二进制字符串
  813. * @return String
  814. * @author fun
  815. * @date 2019年3月27日
  816. */
  817. public static String hexString2binaryString(String hexString) {
  818. if (hexString == null || hexString.length() % 2 != 0)
  819. return null;
  820. String bString = "", tmp;
  821. for (int i = 0; i < hexString.length(); i++) {
  822. tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
  823. bString += tmp.substring(tmp.length() - 4);
  824. }
  825. return bString;
  826. }
  827. }