字符串ASCII,16进制


import java.util.HashMap;
import java.util.Map;


public class StringASCII {

        public static void main(String[] args) {
            String zhiling = returnChecksumZhi("02", "11FD");//
            System.out.println(zhiling);
            String back ="02 30 37 30 30 03 43 41 ";
            Integer integer = backData(back);
            System.out.println("返回数据的计算值="+integer);

        }


    /**
     * @param: [content]或 Integer.parseInt(content, 16);
     * @return: int
     * @description: 十六进制转十进制
     */
    public static int covert(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }
        /**
         * 字符串转换为Ascii
         * @param value
         * @return
         */
        public static String stringToAscii(String value) {
            StringBuffer sbu = new StringBuffer();
            char[] chars = value.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if(i != chars.length - 1)
                {
                    sbu.append((int)chars[i]).append(",");
                }
                else {
                    sbu.append((int)chars[i]);
                }
            }
            return sbu.toString();
        }

        /**
         * Ascii转换为字符串
         * @param value
         * @return
         */
        public static String asciiToString(String value)
        {
            StringBuffer sbu = new StringBuffer();
            String[] chars = value.split(",");
            for (int i = 0; i < chars.length; i++) {
                sbu.append((char) Integer.parseInt(chars[i]));
            }
            return sbu.toString();
        }

    /**
     * Ascii转换为字符串16进制
     * @param
     * @return
     */
    public static String convertStringToHex(String str){
        char[] chars = str.toCharArray();
        StringBuffer hex = new StringBuffer();
        for(int i = 0; i < chars.length; i++){
            hex.append(Integer.toHexString((int)chars[i]));
        }
        return hex.toString();
    }
/*
  *31 31 46 34
*   11F4
*/
    public static String convertHexToString(String hex){
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for( int i=0; i<hex.length()-1; i+=2 ){
            String output = hex.substring(i, (i + 2));
            int decimal = Integer.parseInt(output, 16);
            sb.append((char)decimal);
            temp.append(decimal);
        }
        return sb.toString();
    }

    /**
     * 16进制求和
     * @param hexdata
     * @return
     */

    public static 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 Integer.toHexString(total);
    }

    /**
     *
     * @param dizhi 地址
     * @param hexdata 元件D2200
     * @return 指令 加校验位
     */

    public static String returnChecksumZhi(String dizhi ,String hexdata) {
        String a = convertStringToHex(hexdata);//ASCII字节转16进制字节
        a=  a.substring(0,2)+" "+  a.substring(2,4)+" "+ a.substring(4,6)+" "+ a.substring(6,8);
        String sdata =" 30 "+a+" 30 32 03 ";//加空格方便计算校验位
        String uSum = makeChecksum(sdata);//校验位求和
        if (uSum.length()>=3){//如果求和大于2位数取低位
            String substring = uSum.substring(1, 3);//取后两位
            String s1 = convertStringToHex(substring);//ASCII字节转16进制字节
            sdata+=s1.substring(0,2)+" "+  s1.substring(2,4);//最终指令
        }
        return dizhi+sdata;
    }




    /**
     *
     * @param back 返回的数据
     * @return 十进制
     */

    public static Integer backData(String back ) {
        String trim = back.replace(" ","");//去空格
        String s5 = convertHexToString(trim.substring(2,10));//获取返回数据转成16进制字节
        String zuihzong = s5.substring(2,4)  +s5.substring(0,2);//高低位换位
        int i = Integer.parseInt(zuihzong, 16);//转成10
        System.out.println(i);
        return i;
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值