概述
TLV编码是按 Tag Length Value格式进行编码的 一段码流中的信元用tag标识,tag在码流中唯一不重复 length表示信元value的长度 value表示信元的值
码流以某信元的tag开头 ,tag固定占一个字节,length固定占两个字节,字节序为小端序。
输入:
31
32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC
输出
32 33
说明:
需要解析的信源的tag是31
从码流的起始处开始匹配,tag为32的信元长度为1(01 00,小端序表示为1)
第二个信元的tag为90 其长度为2
第三个信元的tag为30 其长度为3
第四个信元的tag为31 其长度为2(02 00)
所以返回长度后面的两个字节即可 为 32 33
/***
* @Description: 码流由信元组成。信元:Tag(1Byte) + Length(2Byte,小端序) + Value
Input:
Tag:31
码流:32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC
Output:
Value:32 33
* @version: 0.1.0
* @Author: Panda-Young
* @Date: 2022-07-03 16:25:47
* @Copyright (c) 2022 by Panda-Young, All Rights Reserved.
*/
#include <bits/stdc++.h>
using namespace std;
int Hex_to_Dec(string str)
{
int lenggth = 0;
for (int i = 0; i < 4; i++) {
if (str[i] >= 'A' && str[i] <= 'F') {
lenggth += (str[i] - 55) * pow(16, 3-i);
} else {
lenggth += (str[i] - 48) * pow(16, 3-i);
}
}
return lenggth;
}
int main ()
{
string target;
cin >> target;
cin.get(); // enter
string in_str;
getline(cin, in_str);
stringstream ss(in_str);
string s;
vector<string> tmp;
while (getline(ss, s, ' ')) {
tmp.push_back(s);
}
string lenstr;
for (int i = 0; i < tmp.size();) {
lenstr.clear();
lenstr.push_back(tmp[i+2][0]);
lenstr.push_back(tmp[i+2][1]);
lenstr.push_back(tmp[i+1][0]);
lenstr.push_back(tmp[i+1][1]);
int value_len = Hex_to_Dec(lenstr);
if (tmp[i] == target) {
for (int k = 0; k < value_len - 1; k++) {
cout << tmp[i + 3 + k] << ' ';
}
cout << tmp[i+2+value_len] << endl;
return 0;
}
i += (3 + value_len);
}
return 0;
}
最后
以上就是霸气雨为你收集整理的tlv编码的全部内容,希望文章能够帮你解决tlv编码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复