Skip to content

Commit

Permalink
增加crc
Browse files Browse the repository at this point in the history
  • Loading branch information
langcoocl committed Jul 22, 2021
1 parent fa43aeb commit dda46d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/src/main/java/com/xuj/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected void onCreate(Bundle savedInstanceState) {

List<KvEntity> kventity = da.query(tableName, " * ", "", null, null, KvEntity.class);
for (KvEntity entity : kventity) {
Log.e("TAG", "onCreate: " + entity + toString());
Log.e("TAG", "onCreate: " + entity.toString());
}

//执行sql语句
Expand Down
34 changes: 34 additions & 0 deletions xujlibrary/src/main/java/com/xuj/lib/CrcTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xuj.lib;

public class CrcTool {


public static byte getCrcLow(byte[] body) {
byte crcLow = getCrc(body)[1];
return crcLow;
}

public static byte[] getCrc(byte[] body) {
int i = CRC16_XMODEM(body);
byte[] bytes1 = TextHelper.intToByte2(i);
return bytes1;
}


public static int CRC16_XMODEM(byte[] buffer) {
int wCRCin = 0x0000; // initial value 65535
int wCPoly = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (byte b : buffer) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7 - i) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xffff;
return wCRCin ^= 0x0000;
}

}
6 changes: 6 additions & 0 deletions xujlibrary/src/main/java/com/xuj/lib/TextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public static String byteToHexStringAddSpace(byte[] bArray) {
return sb.toString();
}


public static byte[] intToByte2LH(int i) {
byte[] bytes = intToByte2(i);
byte[] re=new byte[]{bytes[1],bytes[0]};
return re;
}
/**
*hexstr转字节数组
*/
Expand Down

0 comments on commit dda46d1

Please sign in to comment.