1:什么是“TLV格式”?
答:TLV格式数据是指由Tag,Length,Value组成的可变的格式数据,简单可以理解为特格式的一个数组
- T可以理解为- Tag(标签)或- Type(类型),用于标识标签或者编码格式信息;
- L定义数据的长度;
- V表示实际的数据。
示例: 01 03 01 02 03 02 05 F1 F2 F3 F4 F5
说明:示例中的这组数据共有2组TLV格式的数据
解析:第一组:T = 01;L = 03;V = 01 02 03
第二组:T = 02;L = 05;V = F1 F2 F3 F4 F5
2:如何处理为java常用的格式?
常用方式先转换为对象数组,在对对象进行解析得到想要的数据
tlv对象
package com.entity;
public class Tlv {
    /** 子域Tag标签 */
    private Integer tag;
    /** 子域取值的长度 */
    private Integer length;
    /** 子域取值 */
    private byte[] value;
    public Tlv(Integer tag, Integer length, byte[] value) {
        this.length = length;
        this.tag = tag;
        this.value = value;
    }
    public Integer getTag() {
        return tag;
    }
    public void setTag(Integer tag) {
        this.tag = tag;
    }
    public Integer getLength() {
        return length;
    }
    public void setLength(Integer length) {
        this.length = length;
    }
    public byte[] getValue() {
        return value;
    }
    public void setValue(byte[] value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "tag=[" + this.tag + "]," + "length=[" + this.length + "]," + "value=[" + this.value + "]";
    }
}
将TLV格式转换为对象数组
package com.common.utils;
public abstract class TlvUtils {
    /**
     * 将byte[]转换为TLV对象列表
     * @param paramsByte tlv格式的byte数组
     * @param position tlv数据起始位置
     * @return
     */
    public static List<Tlv> builderTlvList(byte[] paramsByte,int position) {
        List<Tlv> tlvs = new ArrayList<Tlv>();
        while (position != paramsByte.length) {
            Integer _tag = paramsByte[position] & 0xFF;
            if(_tag == 0){
                break;
            }
            Integer _length = paramsByte[position+1] & 0xFF;
            byte[] _value = Arrays.copyOfRange(paramsByte,position+2,position+2+_length);
            position = position + 2 +_length;
            tlvs.add(new Tlv(_tag, _length, _value));
        }
        return tlvs;
    }
    public static void main(String[] args){
        byte[] paramsByte = new byte[]{};
        System.out.println(JsonUtils.toJson(builderTlvList(paramsByte,0)));
    }
}










