栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java处理TLV格式数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java处理TLV格式数据

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 {

    
    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 {

    
    public static List builderTlvList(byte[] paramsByte,int position) {
        List tlvs = new ArrayList();
        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)));
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号