按约定规则解析16进制为中文或者英文

参考文章 Java 十六进制(Hex)与byte数组之间的转换

总体思路:
16进制   --->  转为byte字节 
 		 --->  取具体约定的字节个数组成字节数组  
 		 --->  调用 new String(nameBytes,"GB18030") 方法获得中文或者英文
import net.sf.json.JSONArray;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 解析16进制的报文数组数据
 **/
public class ParseHex {

    /**
     * 解析打印数组,返回解析后的数组
     * @param printData 16进制打印数据
     * @param filePath  读取文件的路径
     * @param <T>
     * @return 解析后的集合
     * @throws IOException
     */
    public static <T> List<T> parsePrintDataToList(String printData,String filePath) throws IOException {
        byte[] printBytes = hexToByteArray(printData);
        System.out.println(Arrays.toString(printBytes));
        byte index = printBytes[0];
        byte[] nameBytes = new byte[index];
        Map<String,Object> map = new HashMap<>(16);
        if (index >= 0) { System.arraycopy(printBytes, 1, nameBytes, 0, index); }
        String name = new String(nameBytes,"GB18030");
        System.out.println(name);
        List<String> fields = Files.lines(FileSystems.getDefault().getPath(filePath))
                .map(str -> str.substring(0,1).toLowerCase() + str.substring(1))
                .collect(Collectors.toList());
        System.out.println("List<String> fields = " +fields);

        int current = 1+index;
        List<Map<String,Object>> list = new ArrayList<>() ;
        int fieldIndex =0;
        while (current<printBytes.length) {
            current = current + 3;
            int length = (int)printBytes[current];
            current = current+1;
            byte[] contentBytes = new byte[length];
            if (length >= 0) {
                System.arraycopy(printBytes, current, contentBytes, 0, length);
            }
            String content = new String(contentBytes,"GB18030");
            map.put(fields.get(fieldIndex),content);
            fieldIndex = fieldIndex +1;
            current = current + length;
        }
        list.add(map);
        /*list.forEach(System.out::println);
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray);
        String s = jsonArray.toString();*/
        return (List<T>) list;
    }

    private static byte[] hexToByteArray(String inHex) {
        int hexLen = inHex.length();
        byte[] result;
        if (hexLen % 2 == 1){
            hexLen++;
            result = new byte[(hexLen / 2)];
            inHex = "0" + inHex;
        }else {
            result = new byte[(hexLen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexLen ; i += 2 ){
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    private static byte hexToByte(String inHex) {
        return (byte)Integer.parseInt(inHex,16);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JackHuan_code

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

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

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

打赏作者

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

抵扣说明:

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

余额充值