作者:朱克锋
邮箱:[email protected]
转载请注明出处:http://blog.csdn.net/linux_zkf
TLV是一种可变格式,意思就是:
Type类型, Lenght长度,Value值;
Type和Length的长度固定,一般那是2、4个字节(这里统一采用4个字节);
Value的长度有Length指定;
解码方法:
1. 读取type 用ntohl转换为主机字节序得到类型,指针偏移+4
2. 读取lengh用ntohl转换为主机字节序得到长度;指针偏移+4
3. 根据得到的长度读取value,若value数据类型为int、char、short,用ntohl转换为主机字节序,指针偏移+4;若value数据类型为字符串类型,指针偏移+length
类型(Type)字段是关于标签和编码格式的信息;
长度 (Length)字段定义数值的长度;
内容(Value)字段表示实际的数值。
因此,一个编码值又称TLV(Type,Length,Value)三元组。编码可以是基本型或结构型,如果它表示一个简单类型的、完整的显式值,那么编码就是基本型 (primitive);如果它表示的值具有嵌套结构,那么编码就是结构型(constructed)。
以上是对tlv的简单解释,从网上搜集的资源来看,用C、C++来写的开源软件都非常的复杂,我这里使用了很少的OC代码实现了一个简单实用的TLV解析库,供各位参考使用
TLV数据结构
//
// TLV.h
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TLV : NSObject
@property (nonatomic, assign) NSInteger length;
@property (nonatomic, retain) NSString *value;
@property (nonatomic, retain) NSString *tag;
@end
//
// TLV.m
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import "TLV.h"
@implementation TLV
@synthesize tag;
@synthesize value;
@synthesize length;
@end
解析数据结构
//
// LPositon.h
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface LPositon : NSObject
@property (nonatomic,assign) NSInteger vl;
@property (nonatomic,assign) NSInteger position;
@end
//
// LPositon.m
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import "LPositon.h"
@implementation LPositon
@synthesize vl;
@synthesize position;
@end
解析代码
//
// SAXUnionFiled55Utils.h
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TLV.h"
#import "LPositon.h"
@interface TLVParseUtils : NSObject
-(NSArray*) saxUnionField55_2List:(NSString*) hexfiled55;
@end
//
// SAXUnionFiled55Utils.m
// CashBox
//
// Created by ZKF on 13-11-18.
// Copyright (c) 2013年 ZKF. All rights reserved.
//
#import "TLVParseUtils.h"
@implementation TLVParseUtils
-(NSArray*) saxUnionField55_2List:(NSString*) hexfiled55
{
if (nil == hexfiled55) {
}
return [[[self builderTLV:hexfiled55] retain] autorelease];
}
-(NSArray*) builderTLV:(NSString *)hexString
{
NSMutableArray *arr = [[[NSMutableArray alloc] initWithCapacity:10] autorelease];
int position = 0;
while (position != hexString.length) {
NSString * _hexTag = [self getUnionTag:hexString P:position];
NSLog(@"hex tag :%@",_hexTag);
if ([_hexTag isEqualToString:@"00"] || [_hexTag isEqualToString:@"0000"]) {
position += _hexTag.length;
continue;
}
position += _hexTag.length;
LPositon *l_position = [[[self getUnionLAndPosition:hexString P:position] retain] autorelease];;
int _vl = l_position.vl;
NSLog(@"value len :%i",_vl);
position = l_position.position;
NSString* _value = [hexString substringWithRange:NSMakeRange(position, _vl * 2)];
NSLog(@"value :%@",_value);
position = position + _value.length;
TLV *tlv = [[[TLV alloc] init] autorelease];
tlv.tag = _hexTag;
tlv.value = _value;
tlv.length = _vl;
[arr addObject:tlv];
}
return arr;
}
int ChangeNum(char * str,int length)
{
char revstr[128] = {0}; //根据十六进制字符串的长度,这里注意数组不要越界
int num[16] = {0};
int count = 1;
int result = 0;
strcpy(revstr,str);
for (int i = length - 1;i >= 0;i--)
{
if ((revstr[i] >= '0') && (revstr[i] <= '9')) {
num[i] = revstr[i] - 48;//字符0的ASCII值为48
} else if ((revstr[i] >= 'a') && (revstr[i] <= 'f')) {
num[i] = revstr[i] - 'a' + 10;
} else if ((revstr[i] >= 'A') && (revstr[i] <= 'F')) {
num[i] = revstr[i] - 'A' + 10;
} else {
num[i] = 0;
}
result = result+num[i]*count;
count = count*16;//十六进制(如果是八进制就在这里乘以8)
}
return result;
}
-(LPositon *)getUnionLAndPosition:(NSString *)hexString P:(NSInteger) position
{
NSString *firstByteString = [hexString substringWithRange:NSMakeRange(position, 2)];
int i = ChangeNum((char *)[firstByteString UTF8String],2);
NSString * hexLength = @"";
if (((i >> 7) & 1) == 0) {
hexLength = [hexString substringWithRange:NSMakeRange(position, 2)];
position = position + 2;
} else {
// 当最左侧的bit位为1的时候,取得后7bit的值,
int _L_Len = i & 127;
position = position + 2;
hexLength = [hexString substringWithRange:NSMakeRange(position, _L_Len * 2)];
// position表示第一个字节,后面的表示有多少个字节来表示后面的Value值
position = position + _L_Len * 2;
}
LPositon *LP = [[[LPositon alloc] init] autorelease];
LP.vl = ChangeNum((char *)[hexLength UTF8String],2);
LP.position = position;
return LP;
}
-(NSString*) getUnionTag:(NSString* )hexString P:(NSInteger) position
{
NSString* firstByte = [hexString substringWithRange:NSMakeRange(position, 2)];
int i = ChangeNum((char *)[firstByte UTF8String],2);
if ((i & 0x1f) == 0x1f) {
return [hexString substringWithRange:NSMakeRange(position, 4)];
} else {
return [hexString substringWithRange:NSMakeRange(position, 2)];
}
}
@end
测试代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TLVParseUtils*s = [[[TLVParseUtils alloc] init] autorelease];
NSArray *tlvArr = [s saxUnionField55_2List:@"9F260879CC8EC5A09FB9479F2701809F100807010199A0B806019F3704000000009F360201C2950500001800009A031205089C01609F02060000000000005F2A02015682027D009F1A0201569F03060000000000009F3303E0F0F09F34036003029F3501119F1E0832303033313233318405FFFFFFFFFF9F090220069F4104000000019F74064543433030319F631030313032303030308030303030303030"];
NSLog(@"tlv arr :%@",tlvArr);
}
获取源代码请到https://github.com/zhukefeng-ios/TVLParse