BytesUtils.java 24 KB

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