Crc16Utils.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.hb.proj.gather.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class Crc16Utils {
  5. /**
  6. * 一个字节包含位的数量 8
  7. */
  8. private static final int BITS_OF_BYTE = 8;
  9. /**
  10. * 多项式
  11. */
  12. private static final int POLYNOMIAL = 0xA001;
  13. /**
  14. * 初始值
  15. */
  16. private static final int INITIAL_VALUE = 0xFFFF;
  17. /**
  18. * CRC16 编码
  19. * @param bytes 编码内容
  20. * @return 编码结果
  21. */
  22. public static String crc16(int[] bytes) {
  23. int res = INITIAL_VALUE;
  24. for (int data : bytes) {
  25. res = res ^ data;
  26. for (int i = 0; i < BITS_OF_BYTE; i++) {
  27. res = (res & 0x0001) == 1 ? (res >> 1) ^ POLYNOMIAL : res >> 1;
  28. }
  29. }
  30. return Integer.toHexString(revert(res));
  31. }
  32. /**
  33. * 翻转16位的高八位和低八位字节 低位在前 LH
  34. * @param src 翻转数字
  35. * @return 翻转结果
  36. */
  37. private static int revert(int src) {
  38. int lowByte = (src & 0xFF00) >> 8;
  39. int highByte = (src & 0x00FF) << 8;
  40. return lowByte | highByte;
  41. }
  42. public static int getCRC(byte[] bytes) {
  43. int CRC = 0x0000ffff;
  44. int POLYNOMIAL = 0x0000a001;
  45. for(byte b : bytes) {
  46. CRC = CRC ^ (b & 0x000000ff) ;
  47. for(int j = 0; j < 8; j++) {
  48. CRC = (CRC & 0x0001) == 1 ? (CRC >> 1) ^ POLYNOMIAL : CRC >> 1;
  49. }
  50. }
  51. return revert(CRC);
  52. }
  53. public static void main(String[] args) {
  54. // int[] data = new int[]{0x01, 0x03, 0x03, 0xd7, 0x00, 0x01};
  55. // System.out.println(Crc16Utils.crc16(data));
  56. String[] addrs= {"1810","188D","190A","1987","1A04","1A81","1AFE","1B7B","1BF8","1C75","1CF2","1D6F","1DEC","1E69","1EE6","1F63","1FE0","205D","20DA","2157","21D4","2251","22CE","234B","23C8","2445","24C2","253F","25BC","2639","26B6","2733","27B0","282D","28AA","2927","29A4","2A21","2A9E","2B1B"};
  57. int[] cmds=null;
  58. int ad1=0,ad2=0;
  59. String s1,s2=null,crc=null;
  60. String cmdstr=null;
  61. for(String adr : addrs) {
  62. ad1=Integer.parseInt(adr.substring(0,2),16);
  63. ad2=Integer.parseInt(adr.substring(2,4),16);
  64. cmds=new int[] {0x01,0x03,ad1,ad2,0x00,0x7d};
  65. crc=Crc16Utils.crc16(cmds);
  66. System.out.println(crc);
  67. s1=(ad1>0x7f?"(byte)0x":"0x")+adr.substring(0,2);
  68. s2=(ad2>0x7f?"(byte)0x":"0x")+adr.substring(2,4);
  69. cmdstr="0x01,0x03,"+s1+","+s2+",0x00,0x7d";
  70. if(crc.length()>3) {
  71. ad1=Integer.parseInt(crc.substring(0,2),16);
  72. ad2=Integer.parseInt(crc.substring(2,4),16);
  73. s1=(ad1>0x7f?"(byte)0x":"0x")+crc.substring(0,2);
  74. s2=(ad2>0x7f?"(byte)0x":"0x")+crc.substring(2,4);
  75. cmdstr+=","+s1+","+s2;
  76. }
  77. System.out.println(cmdstr);
  78. }
  79. // System.out.println(Integer.toHexString(Crc16Utils.getCRC(data2)));
  80. }
  81. }