package com.hboxs.serialport.plc.util;

import java.util.List;

public class ChecksumUtils {
    public static byte[] calculate(List<Byte> data) {
        byte sum = 0x00;
        for (byte b : data) {
            sum += b;
        }

        // 转16进制
        String hexString = Integer.toHexString(sum & 0x00FF).toUpperCase();

        if (hexString.length() == 1) {
            return new byte[]{(byte) '0', (byte) hexString.charAt(0)};
        } else {
            return new byte[]{(byte) hexString.charAt(0), (byte) hexString.charAt(1)};
        }
    }
}
