我的开源项目:一种TLV编解码器的实现

简介:

一个TLV对象示例如下:


wKioL1ZgS5TjVAGNAAAR9aYejqE708.png 

 

多个TLV对象可以连接起来,组成一个大的buffer:


wKioL1ZgS_iTTHB-AAAUS1zKw08795.png

一个TLV对象内部也可以嵌套另一个TLV对象:


wKiom1ZyvGfxic3fAAAcRRlQ9is443.png


我希望能提供一套API接口(C/C++/Java/其他语言),可以方便地完成TLV格式的编码和解码,项目地址如下所示:


https://github.com/Jhuster/TLV


其中,C++版本已经实现(更新:C/Java/Android版本也均已实现),介绍如下:


1. TLV编码的接口


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//put one TLV box
bool  PutBoolValue( int  type, bool  value);
bool  PutCharValue( int  type, char  value);
bool  PutShortValue( int  type, short  value);
bool  PutIntValue( int  type, int  value);
bool  PutLongValue( int  type, long  value);
bool  PutLongLongValue( int  type, long  long  value);
bool  PutFloatValue( int  type, float  value);
bool  PutDoubleValue( int  type, double  value);
bool  PutStringValue( int  type, char  *value);
bool  PutStringValue( int  type, const  std::string &value);
bool  PutBytesValue( int  type,unsigned  char  *value, int  length);
bool  PutObjectValue( int  type, const  TlvBox& value);          
 
//do encode
bool  Serialize(); 
 
//access encoded buffer and length
unsigned  char  * GetSerializedBuffer()  const ;
int  GetSerializedBytes()  const ;


2. TLV解码的接口


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//do decode
bool  Parse( const  unsigned  char  *buffer, int  buffersize); 
 
//get one TLV box
bool  GetBoolValue( int  type, bool  &value)  const ;
bool  GetCharValue( int  type, char  &value)  const ;
bool  GetShortValue( int  type, short  &value)  const ;
bool  GetIntValue( int  type, int  &value)  const ;
bool  GetLongValue( int  type, long  &value)  const ;
bool  GetLongLongValue( int  type, long  long  &value)  const ;
bool  GetFloatValue( int  type, float  &value)  const ;
bool  GetDoubleValue( int  type, double  &value)  const ;
bool  GetStringValue( int  type, char  *value, int  &length)  const ;
bool  GetStringValue( int  type,std::string &value)  const ;
bool  GetBytesValue( int  type,unsigned  char  *value, int  &length)  const ;
bool  GetBytesValuePtr( int  type,unsigned  char  **value, int  &length)  const ;
bool  GetObjectValue( int  type,TlvBox& value)  const ;




本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1719378,如需转载请自行联系原作者
相关文章
|
存储 缓存 安全
10款轻量型的嵌入式GUI库分享
10款轻量型的嵌入式GUI库分享
2193 1
|
7月前
|
移动开发 前端开发 Java
微信直连商户公众号 JSAPI 支付,详细教程+源码
JSAPI 支付用于微信公众号内的网页调起微信收银台,常见于在公众号菜单、文章页或 H5 活动页中完成支付。该方式依赖微信内置浏览器环境,非微信浏览器无法调起。
883 1
|
存储 开发工具 数据安全/隐私保护
git报错The project you were looking for could not be found 解决方式
git报错The project you were looking for could not be found 解决方式
4555 1
【RTThread】修改Finsh打印串口波特率
【RTThread】修改Finsh打印串口波特率
640 0
|
安全 网络虚拟化
什么是交换机端口分析仪 (SPAN)?
【4月更文挑战第12天】
944 0
|
数据挖掘 Linux 测试技术
Linux中输入所有命令都提示“command not found”,bashrc环境变量异常的解决方案
Linux中输入所有命令都提示“command not found”,bashrc环境变量异常的解决方案
|
缓存 运维 Linux
Centos运维之切换国内yum镜像源
Centos运维之切换国内yum镜像源
4060 0
|
开发工具 iOS开发 MacOS
iOS 逆向编程(二十)class-dump 安装与使用(如何导出APP头文件流程)
iOS 逆向编程(二十)class-dump 安装与使用(如何导出APP头文件流程)
1912 0