/**
* 异或运算和
* @param bytes
* @return
*/
public static byte[] byteOrbyte(byte[] bytes){
byte[] orbyte = new byte[1];
byte value = bytes[0];
for (int i = 1; i < bytes.length; i++) {
value = (byte) (value^bytes[i]);
}
orbyte[0] = value;
return orbyte;
}
求和
//苏标校验码算法
public static String makeChecksum(String data) {
if (data == null || data.equals("")) {
return "";
}
int total = 0;
int len = data.length();
int num = 0;
while (num < len) {
String s = data.substring(num, num + 2);
System.out.println(s);
total += Integer.parseInt(s, 16);
num = num + 2;
}
/**
* 用256求余最大是255,即16进制的FF
*/
int mod = total % 256;
String hex = Integer.toHexString(mod);
len = hex.length();
// 如果不够校验位的长度,补0,这里用的是两位校验
if (len < 2) {
hex = "0" + hex;
}
return hex.toUpperCase();
}