TLV: TLV format data refers to data consisting of Tag, Length, Value.Specific instructions are as follows:
The tag tag attribute is bit, which is represented in hexadecimal and takes up 1-2 bytes of length.For example, "9F33" is a tag tag tag that takes up two bytes."95" is a tag tag tag that takes up one byte.If the first byte of the tag label (Note: Bytes are sorted left to right, the first byte is the leftmost byte.The same is true for bit collation.)The last four bits are "1111", which means the tag takes up two bytes, such as "9F33"; otherwise it takes up one byte, such as "95".(
The attribute of subdomain length (L itself) is also bit, accounting for 1-3 bytes.The encoding rules are as follows:
(a) When the leftmost bit bit bit (bit8) of the leftmost byte of the L field is 0, the L field occupies one byte, and its next seven bits (bit7-bit1) represent the length of the subdomain value, and binary numbers are decimal numbers of the length of the subdomain value.For example, if a domain has a value of 3 bytes, its subdomain value length is expressed as "00000011".Therefore, if the length of the subdomain value is between 1 and 127 bytes, then the L field itself takes up only one byte.(
b) When the leftmost bit bit bit of the leftmost byte in the L field (bit8) is 1, the L field occupies more than one byte, then how many bytes it occupies are represented by the decimal values of the next seven bits of the leftmost byte (that is, bit7 to bit1).For example, if the leftmost byte is 10000010, the L field has two bytes after it.Decimal of its subsequent bytes
Value represents the length of the subdomain value.For example, if the L field is "10000001 1111 1111", that means the subdomain takes up 255 bytes.Therefore, if the length of the subdomain value is 127 to 255 bytes, then the L field itself needs to take up two bytes.(
The parsing code is as follows:
- public class Tlv {
- /** Subdomain Tag Label*/
- private String tag;
- /** Length of subdomain value*/
- private int length;
- /** Subdomain Value*/
- 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 parsing class:
- /**
- * Convert string to TLV object
- *
- */
- public abstract class TlvUtils {
- /**
- * Convert a hexadecimal string to a list of TLV objects
- * @param hexString
- * @return
- */
- public static List<Tlv> builderTlvList(String hexString) {
- List<Tlv> tlvs = new ArrayList<Tlv>();
- 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;
- }
- /**
- * Convert hexadecimal string to TLV object MAP
- * @param hexString
- * @return
- */
- public static Map<String, Tlv> builderTlvMap(String hexString) {
- Map<String, Tlv> tlvs = new HashMap<String, Tlv>();
- 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;
- }
- /**
- * Return the length of the last 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 {
- //When the leftmost bit is 1, get the last 7 bits.
- int _L_Len = i & 127;
- position = position + 2;
- hexLength = hexString.substring(position, position + _L_Len * 2);
- //position represents the first byte, followed by how many bytes to represent the subsequent Value
- position = position + _L_Len * 2;
- }
- return new LPositon(Integer.parseInt(hexLength, 16), position);
- }
- /**
- * Get subdomain Tag Tags
- *
- * @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);
- }
- }
- }