BytesHelp.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. package org.ts.ddcs.tool;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.security.MessageDigest;
  6. import java.util.Calendar;
  7. public class BytesHelp {
  8. /**
  9. * bcd码转int
  10. *
  11. * @param bs cbcd16进制数组
  12. * @param off 起始位置
  13. * @param len 长度
  14. * @return int
  15. */
  16. public static int cbcd2int(byte[] bs, int off, int len) {
  17. int v = 0;
  18. for (int i = off; i < (off + len); i++) {
  19. v = (v * 100) + (((bs[i] & 0xFF) >> 4) * 10) + (bs[i] & 0x0F);
  20. }
  21. return v;
  22. }
  23. /**
  24. * int转bcd码
  25. *
  26. * @param i int数值
  27. * @param len (bcd长度)
  28. * @return byte[] cbcd16进制数组
  29. */
  30. public static byte[] int2cbcd(int i, int len) {
  31. byte[] bs = new byte[len];
  32. int temp;
  33. for (int j = len - 1; j >= 0; j--) {
  34. temp = i % 100;
  35. bs[j] = (byte) (((temp / 10) << 4) + ((temp % 10) & 0x0F));
  36. i /= 100;
  37. }
  38. return bs;
  39. }
  40. /**
  41. * 数值类型转byte数组(long(8),int(4),short(2))
  42. *
  43. * @param num (long,int,short)
  44. * @param len (8, 4, 2)
  45. * @return
  46. */
  47. public static byte[] number2bytes(long num, int len) {
  48. byte[] bs = new byte[len];
  49. for (int j = 0; j < len; j++) {
  50. bs[len - j - 1] = (byte) (num >> (j * 8));
  51. }
  52. return bs;
  53. }
  54. /**
  55. * byte数组转short
  56. *
  57. * @param bs
  58. * @return
  59. */
  60. public static short bytes2short(byte[] bs) {
  61. short s = (short) (bs[1] & 0xFF);
  62. s |= (bs[0] << 8) & 0xFF00;
  63. return s;
  64. }
  65. /**
  66. * short转byte数组
  67. *
  68. * @param s
  69. * @return
  70. */
  71. public static byte[] short2bytes(short s) {
  72. byte[] bs = new byte[2];
  73. for (int i = 1; i >= 0; i--) {
  74. bs[i] = (byte) (s % 256);
  75. s >>= 8;
  76. }
  77. return bs;
  78. }
  79. /**
  80. * String转bcd byte数组
  81. *
  82. * @param s
  83. * @return
  84. */
  85. public static byte[] string2cbcd(String s) {
  86. if (s.length() % 2 != 0) {
  87. s = "0" + s;
  88. }
  89. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  90. char[] cs = s.toCharArray();
  91. for (int i = 0; i < cs.length; i += 2) {
  92. int high = cs[i] - 48;
  93. int low = cs[i + 1] - 48;
  94. baos.write(high << 4 | low);
  95. }
  96. return baos.toByteArray();
  97. }
  98. /**
  99. * 转数据为字符串
  100. * @param bs
  101. * @param off
  102. * @param len
  103. * @return
  104. */
  105. public static String getHexString(byte[] bs, int off, int len) {
  106. StringBuffer sb = new StringBuffer();
  107. for (int i = off; i < (off + len); i++) {
  108. String hex = Integer.toHexString(bs[i] & 0xFF);
  109. if (hex.length() == 1) {
  110. sb.append('0').append(hex);
  111. } else {
  112. sb.append(hex);
  113. }
  114. // sb.append(' ');
  115. }
  116. return sb.toString();
  117. }
  118. /**
  119. * byte数组转String
  120. *
  121. * @param bs
  122. * @param off
  123. * @param len
  124. * @return
  125. */
  126. public static String bytes2String(byte[] bs, int off, int len) {
  127. return new String(bs, off, len).trim();
  128. }
  129. /**
  130. * String 转 byte[]
  131. *
  132. * @param str
  133. * @param len
  134. * @return
  135. */
  136. public static byte[] string2bytes(String str, int len) {
  137. byte[] bs;
  138. if (str != null) {
  139. bs = str.getBytes();
  140. } else {
  141. bs = new byte[len];
  142. }
  143. return bs;
  144. }
  145. /**
  146. * int 转 byte数组
  147. *
  148. * @param i
  149. * @return
  150. */
  151. public static byte[] int2Bytes(int i) {
  152. return int2Bytes(i, 4);
  153. }
  154. /**
  155. * int 转 byte数组
  156. *
  157. * @param i
  158. * @return
  159. */
  160. public static byte[] intToBytes(int i,int length) {
  161. return int2Bytes(i, length);
  162. }
  163. /**
  164. * int 转 byte数组
  165. *
  166. * @param i
  167. * @param len
  168. * @return
  169. */
  170. public static byte[] int2Bytes(int i, int len) {
  171. byte[] bs = new byte[len];
  172. for (int j = 0; j < len; j++) {
  173. bs[len - j - 1] = (byte) (i >> (j * 8));
  174. }
  175. return bs;
  176. }
  177. public static int byteslowbitToInt(byte[] bytes,int off){
  178. byte[] tmpbuf = new byte[4];
  179. tmpbuf[2] = (byte) (bytes[off] & 0x0f);
  180. tmpbuf[3] = bytes[off+1];
  181. int v = 0;
  182. for (int ii = 0; ii < 4; ii++) {
  183. int shi = (4 - 1 - ii) * 8;
  184. v += (tmpbuf[ii] & 0x000000FF) << shi;
  185. }
  186. return v;
  187. }
  188. /**
  189. * byte数组转int(命令字专用)
  190. *
  191. * @param bytes
  192. * @return
  193. */
  194. public static int bytes2Int(byte[] bytes, int off, int len) {
  195. int v = 0;
  196. int pos = off + len - 1;
  197. if (pos >= 0) {
  198. v |= (bytes[pos] & 0xFF);
  199. }
  200. pos--;
  201. if (pos >= 0) {
  202. v |= ((bytes[pos] << 8) & 0xFF00);
  203. }
  204. pos--;
  205. if (pos >= 0) {
  206. v |= ((bytes[pos] << 16) & 0xFF0000);
  207. }
  208. pos--;
  209. if (pos >= 0) {
  210. v |= ((bytes[pos] << 24) & 0xFF000000);
  211. }
  212. return v;
  213. }
  214. // bytes补位用
  215. public static byte[] string2Bytes(String src, int len, int max) {
  216. byte[] dest = new byte[max];
  217. if (src == null) {
  218. return dest;
  219. }
  220. while (src.length() < len) {
  221. src += " ";
  222. }
  223. byte[] temp = src.getBytes();
  224. System.arraycopy(temp, 0, dest, 0, temp.length >= len ? len
  225. : temp.length);
  226. return dest;
  227. }
  228. public static byte[] getBytesFromFile(File file) {
  229. byte[] ret = null;
  230. try {
  231. if (file == null) {
  232. return null;
  233. }
  234. FileInputStream in = new FileInputStream(file);
  235. ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
  236. byte[] b = new byte[4096];
  237. int n;
  238. while ((n = in.read(b)) != -1) {
  239. out.write(b, 0, n);
  240. }
  241. in.close();
  242. out.close();
  243. ret = out.toByteArray();
  244. } catch (Exception e) {
  245. return null;
  246. }
  247. return ret;
  248. }
  249. public static String getMD5Digest(String str) {
  250. return getMD5Digest(str.getBytes());
  251. }
  252. public static String getMD5Digest(byte[] bytes) {
  253. MessageDigest messageDigest = null;
  254. try {
  255. messageDigest = MessageDigest.getInstance("MD5");
  256. messageDigest.reset();
  257. messageDigest.update(bytes);
  258. } catch (Exception e) {
  259. e.printStackTrace();
  260. }
  261. byte[] byteArray = messageDigest.digest();
  262. StringBuffer md5StrBuff = new StringBuffer();
  263. for (int i = 0; i < byteArray.length; i++) {
  264. if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
  265. md5StrBuff.append("0").append(
  266. Integer.toHexString(0xFF & byteArray[i]));
  267. else
  268. md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
  269. }
  270. return md5StrBuff.toString();
  271. }
  272. public static String leftFill(String src, int len, char fillChar) {
  273. int length = src.length();
  274. if (src == null || len < length)
  275. return src;
  276. String result = src;
  277. for (int i = length; i < len; i++) {
  278. result = String.valueOf(fillChar) + result;
  279. }
  280. return result;
  281. }
  282. /**
  283. * 将字符串拼接成指定长度的字符串,不足的补空格
  284. *
  285. * @param str 字符串
  286. * @param length
  287. * @return
  288. */
  289. public static String spliceSpaces(String str, int length) {
  290. int strLen = str.length();
  291. if (strLen > length) {
  292. str = "";
  293. for (int i = 0; i < length; i++) {
  294. str += " ";
  295. }
  296. } else {
  297. for (int i = 0; i < (length - strLen); i++) {
  298. str += " ";
  299. }
  300. }
  301. return str;
  302. }
  303. /**
  304. * 将字符串拼接成指定长度的字符串,不足的补0
  305. *
  306. * @param str 字符串
  307. * @param length
  308. * @return
  309. */
  310. public static String spliceZero(String str, int length) {
  311. int strLen = str.length();
  312. if (strLen > length) {
  313. str = "";
  314. for (int i = 0; i < length; i++) {
  315. str += "0";
  316. }
  317. } else {
  318. for (int i = 0; i < (length - strLen); i++) {
  319. str = "0" + str;
  320. }
  321. }
  322. return str;
  323. }
  324. public static byte[] arrayApend(byte[] a, byte[] b) {
  325. int a_len = (a == null ? 0 : a.length);
  326. int b_len = (b == null ? 0 : b.length);
  327. byte[] c = new byte[a_len + b_len];
  328. if (a_len == 0 && b_len == 0) {
  329. return null;
  330. } else if (a_len == 0) {
  331. System.arraycopy(b, 0, c, 0, b.length);
  332. } else if (b_len == 0) {
  333. System.arraycopy(a, 0, c, 0, a.length);
  334. } else {
  335. System.arraycopy(a, 0, c, 0, a.length);
  336. System.arraycopy(b, 0, c, a.length, b.length);
  337. }
  338. return c;
  339. }
  340. public static byte[] arrayApendbyte(byte[] a, byte b) {
  341. int a_len = (a == null ? 0 : a.length);
  342. byte[] c = new byte[a_len + 1];
  343. if (a_len == 0) {
  344. return null;
  345. } else {
  346. System.arraycopy(a, 0, c, 0, a.length);
  347. System.arraycopy(b, 0, c, a.length, 1);
  348. }
  349. return c;
  350. }
  351. /**
  352. * 获取字符Ascii值
  353. *
  354. * @param str
  355. * @return
  356. */
  357. public static int getAsciiValue(String str) {
  358. if (str.length() != 1) {
  359. return 0;
  360. }
  361. char[] ch = str.toCharArray();
  362. return ch[0];
  363. }
  364. /**
  365. * 获取字符Ascii值 16
  366. *
  367. * @param str
  368. * @return
  369. */
  370. public static String getAsciiHexValueS(String str) {
  371. char[] ch = str.toCharArray();
  372. StringBuffer retStr = new StringBuffer();
  373. for (int i = 0; i < ch.length; i++) {
  374. retStr.append(Integer.toHexString(ch[i]));
  375. }
  376. return retStr.toString();
  377. }
  378. /**
  379. * 获取字符Ascii值
  380. *
  381. * @param str
  382. * @return
  383. */
  384. public static String getAsciiValueS(String str) {
  385. char[] ch = str.toCharArray();
  386. StringBuffer retStr = new StringBuffer();
  387. for (int i = 0; i < ch.length; i++) {
  388. retStr.append((int) ch[i]);
  389. }
  390. return retStr.toString();
  391. }
  392. public static String getAsciiStr(int d) {
  393. char e = (char) d;
  394. return e + "";
  395. }
  396. public static String byte2HexStr(byte[] b) {
  397. String stmp = "";
  398. StringBuilder sb = new StringBuilder("");
  399. for (int n = 0; n < b.length; n++) {
  400. stmp = Integer.toHexString(b[n] & 0xFF);
  401. sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
  402. //sb.append(" ");
  403. }
  404. return sb.toString().toUpperCase().trim();
  405. }
  406. public static String byte2HexStr(byte[] b, int pos, int length) {
  407. String stmp = "";
  408. StringBuilder sb = new StringBuilder("");
  409. for (int n = 0; n < length; n++) {
  410. stmp = Integer.toHexString(b[n + pos] & 0xFF);
  411. sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
  412. }
  413. return sb.toString().toUpperCase().trim();
  414. }
  415. public static byte[] hexStr2Bytes(String hexString) {
  416. hexString = hexString.toLowerCase();
  417. int remainder = hexString.length() % 2;
  418. if(remainder>0){
  419. hexString ="0"+hexString;
  420. }
  421. final byte[] byteArray = new byte[hexString.length()/2];
  422. int k = 0;
  423. for (int i = 0; i < byteArray.length; i++) {
  424. //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
  425. //将hex 转换成byte "&" 操作为了防止负数的自动扩展
  426. // hex转换成byte 其实只占用了4位,然后把高位进行右移四位
  427. // 然后“|”操作 低四位 就能得到 两个 16进制数转换成一个byte.
  428. //
  429. byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
  430. byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
  431. byteArray[i] = (byte) (high << 4 | low);
  432. k += 2;
  433. }
  434. return byteArray;
  435. }
  436. public static byte[] subBytes(byte[] src, int begin, int count) {
  437. byte[] bs = new byte[count];
  438. for (int i = begin; i < begin + count; i++) bs[i - begin] = src[i];
  439. return bs;
  440. }
  441. //CRC校验运算
  442. public static byte GetCRCByte(byte[] data) {
  443. int crc = 0x0; // 初始值为0
  444. for (byte bt : data) {
  445. crc = crc ^ bt;
  446. for (int j = 1; j <= 8; j++) {
  447. if ((crc & 0x80) == 0x80)
  448. crc = (crc << 1) ^ 0xE5;// 多项式值为E5,被校验值左移
  449. else
  450. crc = crc << 1;
  451. }
  452. }
  453. return (byte) crc;
  454. }
  455. //将16进制字符串转换为16进制byte数组
  456. public static byte[] HexString2Bytes(String src) {
  457. byte[] ret = new byte[8];
  458. byte[] tmp = src.getBytes();
  459. for (int i = 0; i < 8; i++) {
  460. ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
  461. }
  462. return ret;
  463. }
  464. public static byte uniteBytes(byte src0, byte src1) {
  465. byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
  466. _b0 = (byte) (_b0 << 4);
  467. byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
  468. byte ret = (byte) (_b0 ^ _b1);
  469. return ret;
  470. }
  471. //16进制字符串转换为2进制字符串
  472. public static String hexString2binaryString(String hexString) {
  473. if (hexString == null || hexString.length() % 2 != 0)
  474. return null;
  475. String bString = "", tmp;
  476. for (int i = 0; i < hexString.length(); i++) {
  477. tmp = "0000"
  478. + Integer.toBinaryString(Integer.parseInt(
  479. hexString.substring(i, i + 1), 16));
  480. bString += tmp.substring(tmp.length() - 4);
  481. }
  482. return bString;
  483. }
  484. /**
  485. * 获取时间戳位
  486. *
  487. * @return
  488. */
  489. public static byte[] getTPByte() {
  490. Calendar now = Calendar.getInstance();
  491. //String year = now.get(Calendar.YEAR)+"";
  492. String second = now.get(Calendar.SECOND) + "";
  493. String minute = now.get(Calendar.MINUTE) + "";
  494. String hour = now.get(Calendar.HOUR_OF_DAY) + "";
  495. String day = now.get(Calendar.DATE) + "";
  496. byte[] tpArea;
  497. if (second.length() == 1) {
  498. tpArea = BytesHelp.hexStr2Bytes("0" + second);
  499. } else {
  500. tpArea = BytesHelp.hexStr2Bytes(second);
  501. }
  502. if (minute.length() == 1) {
  503. tpArea = BytesHelp.arrayApend(tpArea,
  504. BytesHelp.hexStr2Bytes("0" + minute));
  505. } else {
  506. tpArea = BytesHelp.arrayApend(tpArea,
  507. BytesHelp.hexStr2Bytes(minute));
  508. }
  509. if (hour.length() == 1) {
  510. tpArea = BytesHelp.arrayApend(tpArea,
  511. BytesHelp.hexStr2Bytes("0" + hour));
  512. } else {
  513. tpArea = BytesHelp.arrayApend(tpArea,
  514. BytesHelp.hexStr2Bytes(hour));
  515. }
  516. if (day.length() == 1) {
  517. tpArea = BytesHelp.arrayApend(tpArea,
  518. BytesHelp.hexStr2Bytes("0" + day));
  519. } else {
  520. tpArea = BytesHelp.arrayApend(tpArea,
  521. BytesHelp.hexStr2Bytes(day));
  522. }
  523. tpArea = BytesHelp.arrayApend(tpArea,
  524. BytesHelp.hexStr2Bytes("00"));
  525. return tpArea;
  526. }
  527. /**
  528. * 单一字节中选择指定位bcd码转换为整数
  529. *
  530. * @param b
  531. * @param highBit 高位,最大7
  532. * @param lowBit 低位,从0开始
  533. * @return
  534. */
  535. public static int bcdToInt(byte b, int highBit, int lowBit) {
  536. int g = highBit - lowBit + 1;
  537. int a = ((int) Math.pow(2, g) - 1) << lowBit;
  538. int b2 = (b & a) >> lowBit;
  539. return b2;
  540. }
  541. public static byte[] crcFun2014(byte[] packet, int pos, int length) {
  542. int n = length;
  543. int crc = 0xFFFF;
  544. for (int b = 0; b < n; b++) {
  545. crc = byteToInteger(packet[pos + b]) ^ crc;
  546. for (int c = 0; c < 8; c++) {
  547. int dd = crc & 0x0001;
  548. if (dd == 1) {
  549. crc = crc >> 1;
  550. crc ^= 0xA001;
  551. } else {
  552. crc = crc >> 1;
  553. }
  554. }
  555. }
  556. return BytesHelp.int2Bytes(crc, 2);
  557. }
  558. public static byte[] crcFun2014(byte[] packet) {
  559. int n = packet.length;
  560. int crc = 0xFFFF;
  561. for (int b = 0; b < n; b++) {
  562. crc = byteToInteger(packet[b]) ^ crc;
  563. for (int c = 0; c < 8; c++) {
  564. int dd = crc & 0x0001;
  565. if (dd == 1) {
  566. crc = crc >> 1;
  567. crc ^= 0xA001;
  568. } else {
  569. crc = crc >> 1;
  570. }
  571. }
  572. }
  573. return BytesHelp.int2Bytes(crc, 2);
  574. }
  575. public static int byteToInteger(byte b) {
  576. int value;
  577. value = b & 0xff;
  578. return value;
  579. }
  580. /**
  581. * 获取发报时间
  582. *
  583. * @return
  584. */
  585. public static byte[] getFBTm() {
  586. Calendar now = Calendar.getInstance();
  587. String year = now.get(Calendar.YEAR) + "";
  588. String month = now.get(Calendar.MONTH) + 1 + "";
  589. String day = now.get(Calendar.DATE) + "";
  590. String hour = now.get(Calendar.HOUR_OF_DAY) + "";
  591. String minute = now.get(Calendar.MINUTE) + "";
  592. String second = now.get(Calendar.SECOND) + "";
  593. byte[] tpArea;
  594. tpArea = BytesHelp.hexStr2Bytes(year.substring(2, year.length()));
  595. if (month.length() == 1) {
  596. tpArea = BytesHelp.arrayApend(tpArea,
  597. BytesHelp.hexStr2Bytes("0" + month));
  598. } else {
  599. tpArea = BytesHelp.arrayApend(tpArea,
  600. BytesHelp.hexStr2Bytes(month));
  601. }
  602. if (day.length() == 1) {
  603. tpArea = BytesHelp.arrayApend(tpArea,
  604. BytesHelp.hexStr2Bytes("0" + day));
  605. } else {
  606. tpArea = BytesHelp.arrayApend(tpArea,
  607. BytesHelp.hexStr2Bytes(day));
  608. }
  609. if (hour.length() == 1) {
  610. tpArea = BytesHelp.arrayApend(tpArea,
  611. BytesHelp.hexStr2Bytes("0" + hour));
  612. } else {
  613. tpArea = BytesHelp.arrayApend(tpArea,
  614. BytesHelp.hexStr2Bytes(hour));
  615. }
  616. if (minute.length() == 1) {
  617. tpArea = BytesHelp.arrayApend(tpArea,
  618. BytesHelp.hexStr2Bytes("0" + minute));
  619. } else {
  620. tpArea = BytesHelp.arrayApend(tpArea,
  621. BytesHelp.hexStr2Bytes(minute));
  622. }
  623. if (second.length() == 1) {
  624. tpArea = BytesHelp.arrayApend(tpArea, BytesHelp.hexStr2Bytes("0" + second));
  625. } else {
  626. tpArea = BytesHelp.arrayApend(tpArea, BytesHelp.hexStr2Bytes(second));
  627. }
  628. return tpArea;
  629. }
  630. //将16进制RTU编码改成10进制
  631. public static String byte2HexMpCdStr(byte[] b) {
  632. String stmp = "";
  633. StringBuilder sb = new StringBuilder("");
  634. for (int n = 0; n < 3; n++) {
  635. stmp = Integer.toHexString(b[n] & 0xFF);
  636. sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
  637. }
  638. int mpcd = 0;
  639. if (sb.toString().startsWith("00")) {//00开头的站点编号按照10进制处理,99的按照16进制处理,为了满足威控的编码格式
  640. String str4 = Integer.toHexString(b[4] & 0xFF);
  641. String mpcdStr = Integer.toHexString(b[3] & 0xFF);
  642. if(str4.length()==1){
  643. mpcdStr += "0"+str4;
  644. }else{
  645. mpcdStr +=str4;
  646. }
  647. //String mpcdStr = Integer.toHexString(b[3] & 0xFF)+Integer.toHexString(b[4] & 0xFF);
  648. mpcd = Integer.parseInt(mpcdStr);
  649. } else {
  650. mpcd = bytes2Int(BytesHelp.subBytes(b, 3, 2), 0, 2) % 10000;
  651. }
  652. sb.append(StringUtil.leftFill(mpcd + "", 4, '0'));
  653. return sb.toString().toUpperCase().trim();
  654. }
  655. //将16进制RTU编码改成10进制(东深厂商专用),编号最后俩个字节颠倒
  656. public static String byte2HexMpCdStrDS(byte[] b) {
  657. String stmp = "";
  658. StringBuilder sb = new StringBuilder("");
  659. for (int n = 0; n < 3; n++) {
  660. stmp = Integer.toHexString(b[n] & 0xFF);
  661. sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
  662. }
  663. int mpcd = 0;
  664. if (sb.toString().startsWith("00")) {//00开头的站点编号按照10进制处理,99的按照16进制处理,为了满足威控的编码格式
  665. String mpcdStr = Integer.toHexString(b[3] & 0xFF) + Integer.toHexString(b[4] & 0xFF);
  666. mpcd = Integer.parseInt(mpcdStr);
  667. } else {
  668. byte[] mpCdTail = new byte[2];
  669. mpCdTail[0] = b[4];
  670. mpCdTail[1] = b[3];
  671. //mpcd = bytes2Int( BytesHelp.subBytes(b, 3, 2),0,2) %10000;
  672. mpcd = bytes2Int(mpCdTail, 0, 2) % 10000;
  673. }
  674. sb.append(StringUtil.leftFill(mpcd + "", 4, '0'));
  675. return sb.toString().toUpperCase().trim();
  676. }
  677. //bdcd十六进字符串转成字节数组
  678. public static byte[] hstrToBytes(String str) {
  679. char[] ca = str.toCharArray();
  680. byte[] databuff = new byte[ca.length / 2];
  681. for (int j = 0; j < ca.length; j += 2) {
  682. byte bb = (byte) (charToByte(ca[j]) << 4 | charToByte(ca[j + 1]));
  683. databuff[j == 0 ? 0 : (j / 2)] = bb;
  684. }
  685. return databuff;
  686. }
  687. public static byte charToByte(char c) {
  688. return (byte) "0123456789ABCDEF".indexOf(c);
  689. }
  690. }