ASCii转码工具类

/**
 * Ascii工具类
 *
 * @author jilke
 */
public class AsciiUtils {

    /**
     * 无符号整数ASC转String
     * @param hex
     * @return
     */
    public static String hexTo10(String hex) {
        String myStr[] = {"a", "b", "c", "d", "e", "f"};
        int result = 0;
        int n = 1;
        for (int i = hex.length() - 1; i >= 0; i--) {
            String param = hex.substring(i, i + 1);
            for (int j = 0; j < myStr.length; j++) {
                if (param.equalsIgnoreCase(myStr[j])) {
                    param = "1" + String.valueOf(j);
                }
            }
            result += Integer.parseInt(param) * n;
            n *= 16;
        }
        return String.valueOf(result);
    }

    /**
     * 有符号asc码转String(业务需要除以10保留一位小数点)
     * @param hexString
     * @return
     */
    public static String hexToDecimal(String hexString) {
        int decimal = Integer.parseInt(hexString, 16);
        if ((decimal & 0x8000) != 0) { // 判断最高位是否为1
            decimal = -(0x10000 - decimal); // 负数处理
        }
        // 除以10保留一位小数点
        double result = (double) decimal / 10.0;
        return String.valueOf(result);
    }

    public static void main(String[] args) {
        String ffc9 = hexToDecimal("FFC9");
        System.out.println(ffc9);
    }

    /**
     * int转asc码string
     * @param decimal
     * @return
     */
    public static String decimalToHex(int decimal) {
        String hexString = Integer.toHexString(decimal);
        if (decimal < 0) {
            hexString = hexString.substring(4); // 截取后四位,因为负数的情况下 toHexString 方法返回的字符串长度为8
            while (hexString.length() < 4) {
                hexString = "0" + hexString; // 在负数的情况下补足为4位
            }
        }
        return hexString.toUpperCase();
    }

    /**
     * int转asc码string
     * @param decimal
     * @param n 长度补零
     * @return
     */
    public static String decimalToHex(int decimal, int n) {
        String hexString = Integer.toHexString(decimal);
        if (decimal < 0) {
            hexString = hexString.substring(4); // 截取后四位,因为负数的情况下 toHexString 方法返回的字符串长度为8
            while (hexString.length() < n) {
                hexString = "0" + hexString; // 在负数的情况下补足为 N 位
            }
        } else {
            while (hexString.length() < n) {
                hexString = "0" + hexString; // 在正数的情况下补足为 N 位
            }
        }
        return hexString.toUpperCase();
    }

    /**
     * VHF语句校验
     * @param sentence 语句内容
     * 1-异或校验异常 MessageVerificationException
     * 2-语句异常 MessageFormatException
     * 3-消息号异常 MessageNoException
     * 4-消息长度异常 MessageLengthException
     * 5-其他异常 Exception
     * @return
     * @throws MessageVerificationException
     * @throws MessageFormatException
     */
    public static boolean checkVhf(String sentence) throws MessageVerificationException, MessageFormatException {
        String[] strs = sentence.split(",");
        if (strs.length > 0 && strs[0].length() == 6 && strs[1].contains("*")) {
            String verification = sentence.substring(sentence.indexOf("*") + 1, sentence.indexOf("*") + 3);
            String checkStr = sentence.substring(1, sentence.indexOf("*"));
            String verificationCode = verification(checkStr);

            if (!verification.equals(verificationCode)) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    public static String verification(String str) {
        int c1 = str.charAt(0);
        for(int i = 1; i < str.length(); ++i) {
            int c = str.charAt(i);
            c1 ^= c;
        }

        String verificationCode = Long.toHexString((long)c1).toUpperCase();
        if (verificationCode.length() < 2) {
            verificationCode = "0" + verificationCode;
        }
        return verificationCode;
    }

    /*
     *
     * VHF报文校验
     *
     */
    public static String verifications(String str) {
        int c1 = str.charAt(0);

        for (int i = 1; i < str.length(); i++) {
            int c = str.charAt(i);

            c1 = c1 ^ c;
        }

        String verificationCode = Long.toHexString(c1).toUpperCase();

        if (verificationCode.length() < 2) {
            verificationCode = "0" + verificationCode;
        }

        return verificationCode;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jilke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值