DLT645校驗位

 

DLT645檢驗位是把檢驗位之前的所有16進制接起來去最後兩位數

/**
 * DLT645校驗碼
 */
public class DLT655 {

    public static String makeCheck(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);
            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()+"16";
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章