package com.gzlh.device.electron.utils; import cn.hutool.core.util.HexUtil; public class CheckNumUtils { public static String calculateChecksum(byte[] uBuff, int uBuffLen) { int uSum = 0; for (int i = 0; i < uBuffLen; i++) { uSum = uSum + (uBuff[i] & 0xFF); } uSum = (~uSum) + 1; return HexUtil.toHex(uSum & 0xFF); } public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } public static String calculateChecksum(String s){ byte[]array= hexStringToByteArray(s); return calculateChecksum(array,array.length); } }