TLV:TLV格式数据是指由Tag,Length,Value组成的数据。具体说明如下:

tag标签的属性为bit,由16进制表示,占1~2个字节长度。例如,“9F33”为一个占用两个字节 的tag标签。而“95”为一个占用一个字节的tag标签。若tag标签的第一个字节(注:字节排序方向为从左往右数,第一个字节即为最左边的字节。bit排序规则同理。)的后四个bit为“1111”,则说明该tag占两个字节,例如“9F33”;否则占一个字节,例“95”。

子域长度(即L本身)的属性也为bit,占1~3个字节长度。具体编码规则如下:

a)  当 L 字段最左边字节的最左 bit 位(即 bit8)为 0,表示该 L 字段占一个字节,它的后续 7个 bit 位(即 bit7~bit1)表示子域取值的长度,采用二进制数表示子域取值长度的十进制数。例如,某个域取值占 3 个字节,那么其子域取值长度表示为“00000011”。所以,若子域取值的长度在 1~127 字节之间,那么该 L 字段本身仅占一个字节。

b)  当 L 字段最左边字节的最左 bit 位(即 bit8)为 1,表示该 L 字段不止占一个字节,那么它到底占几个字节由该最左字节的后续 7 个 bit 位(即 bit7~bit1)的十进制取值表示。例如,若最左字节为 10000010,表示 L 字段除该字节外,后面还有两个字节。其后续字节的十进制

取值表示子域取值的长度。例如,若 L 字段为“1000 0001 1111 1111”,表示该子域取值占255 个字节。所以,若子域取值的长度在 127~255 字节之间,那么该 L 字段本身需占两个字节。

解析代码如下:

public class Tlv {

/** 子域Tag标签 */

private String tag;

/** 子域取值的长度 */

private int length;

/** 子域取值 */

private String value;

public Tlv(String tag, int length, String value) {

this.length = length;

this.tag = tag;

this.value = value;

}

public String getTag() {

return tag;

}

public void setTag(String tag) {

this.tag = tag;

}

public int getLength() {

return length;

}

public void setLength(int length) {

this.length = length;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

@Override

public String toString() {

return "tag=[" + this.tag + "]," + "length=[" + this.length + "]," + "value=[" + this.value + "]";

}

}

TLV解析类:

/**

* 将字符串转换为TLV对象

*

*/

public abstract class TlvUtils {

/**

* 将16进制字符串转换为TLV对象列表

* @param hexString

* @return

*/

public static List builderTlvList(String hexString) {

List tlvs = new ArrayList();

int position = 0;

while (position != StringUtils.length(hexString)) {

String _hexTag = getTag(hexString, position);

position += _hexTag.length();

LPositon l_position = getLengthAndPosition(hexString, position);

int _vl = l_position.get_vL();

position = l_position.get_position();

String _value = StringUtils.substring(hexString, position, position + _vl * 2);

position = position + _value.length();

tlvs.add(new Tlv(_hexTag, _vl, _value));

}

return tlvs;

}

/**

* 将16进制字符串转换为TLV对象MAP

* @param hexString

* @return

*/

public static Map builderTlvMap(String hexString) {

Map tlvs = new HashMap();

int position = 0;

while (position != hexString.length()) {

String _hexTag = getTag(hexString, position);

position += _hexTag.length();

LPositon l_position = getLengthAndPosition(hexString, position);

int _vl = l_position.get_vL();

position = l_position.get_position();

String _value = hexString.substring(position, position + _vl * 2);

position = position + _value.length();

tlvs.put(_hexTag, new Tlv(_hexTag, _vl, _value));

}

return tlvs;

}

/**

* 返回最后的Value的长度

*

* @param hexString

* @param position

* @return

*/

private static LPositon getLengthAndPosition(String hexString, int position) {

String firstByteString = hexString.substring(position, position + 2);

int i = Integer.parseInt(firstByteString, 16);

String hexLength = "";

if (((i >>> 7) & 1) == 0) {

hexLength = hexString.substring(position, position + 2);

position = position + 2;

} else {

// 当最左侧的bit位为1的时候,取得后7bit的值,

int _L_Len = i & 127;

position = position + 2;

hexLength = hexString.substring(position, position + _L_Len * 2);

// position表示第一个字节,后面的表示有多少个字节来表示后面的Value值

position = position + _L_Len * 2;

}

return new LPositon(Integer.parseInt(hexLength, 16), position);

}

/**

* 取得子域Tag标签

*

* @param hexString

* @param position

* @return

*/

private static String getTag(String hexString, int position) {

String firstByte = StringUtils.substring(hexString, position, position + 2);

int i = Integer.parseInt(firstByte, 16);

if ((i & 0x1f) == 0x1f) {

return hexString.substring(position, position + 4);

} else {

return hexString.substring(position, position + 2);

}

}

}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐