<pre name="code" class="java">/**
* 将字符串转换为TLV对象
*
*/
public abstract class TlvUtils {
/**
* 将16进制字符串转换为TLV对象列表
* @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;
}
/**
* 将16进制字符串转换为TLV对象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;
}
/**
* 返回最后的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);
}
}
}
/**
* value的长度
*/
public class LPositon {
private int _vL;
private int _position;
public LPositon(int _vL, int position) {
this._vL = _vL;
this._position = position;
}
public int get_vL() {
return _vL;
}
public void set_vL(int _vL) {
this._vL = _vL;
}
public int get_position() {
return _position;
}
public void set_position(int _position) {
this._position = _position;
}
}
TLV:TLV格式数据是指由Tag,Length,Value组成的数据。具体说明如下:
Type和Length的长度固定,一般那是2、4个字节(这里统一采用4个字节);
Value的长度有Length指定
编码格式:
1. 将类型type转换为网络字节顺序,指针偏移+4
2. 将长度length转换为网络字节顺序,指针偏移+4
3. 若值value数据类型为int、char、short,则将其转换为网络字节顺序,指针偏移+4;若值为字符串类型,写进后,指针偏移+length
……继续处理后面的tlv;
解码格式:(和编码格式相反)
类型(Type)字段是关于标签和编码格式的信息;
长度 (Length)字段定义数值的长度;
内容(Value)字段表示实际的数值。
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 + "]";
}
}
http://my.oschina.net/maxid/blog/206546