网上找了很多都不好用,这个自己测过可用
如需看原理可以看如下连接
点击异或校验工作方式
public static void main(String[] args) {
String code1 = "TYH,0,124,ChEQjIGz+QUYgKjcvMGuDCCEAhIbCAEQjIGz+QUaBwgBEJh1GAEaCAgBENCMARgCEisIBhCMgbP5BRoHCAoQ/BEYARoPCAoQ8vP/ARgCGgcIChCKBhgB";
//ascii码转化位16进制
String a=convertStringToHex(code1);
String code=yihuo(a);
System.out.println(code);
}
public static String yihuo(String content) {
content = change(content);
String[] b = content.split(" ");
int a = 0;
for (int i = 0; i < b.length; i++) {
a = a ^ Integer.parseInt(b[i], 16);
}
if(a<10){
StringBuffer sb = new StringBuffer();
sb.append("0");
sb.append(a);
return sb.toString();
}
return Integer.toHexString(a);
}
public static String change(String content) {
String str = "";
for (int i = 0; i < content.length(); i++) {
if (i % 2 == 0) {
str += " " + content.substring(i, i + 1);
} else {
str += content.substring(i, i + 1);
}
}
return str.trim();
}
public static String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}