BytesUtils.java 22 KB

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