java 16进制求和,也就是16进制字节求二进制和,自己写的逻辑有点问题,不能满足需求,故从网上搜了下,满足了需求,代码如下
public class Jinzhi16Util { public String makeChecksum(String hexdata) { if (hexdata == null || hexdata.equals("")) { return "00"; } hexdata = hexdata.replaceAll(" ", ""); int total = 0; int len = hexdata.length(); if (len % 2 != 0) { return "00"; } int num = 0; while (num < len) { String s = hexdata.substring(num, num + 2); total += Integer.parseInt(s, 16); num = num + 2; } return hexInt(total); } private String hexInt(int total) { int a = total / 256; int b = total % 256; if (a > 255) { return hexInt(a) + format(b); } return format(a) + format(b); } private String format(int hex) { String hexa = Integer.toHexString(hex); int len = hexa.length(); if (len < 2) { hexa = "0" + hexa; } return hexa; } public static void main(String[] args) { //16进制相加 取和 Jinzhi16Util u16 = new Jinzhi16Util(); String s =u16.makeChecksum("0733"); System.out.println(s); } }
运行main方法结果:003a