Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1480898
  • 博文数量: 108
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 997
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-29 09:58
个人简介

兴趣是坚持一件事永不衰竭的动力

文章分类

全部博文(108)

文章存档

2021年(1)

2020年(10)

2019年(19)

2018年(9)

2016年(23)

2015年(43)

2013年(3)

我的朋友

分类: Java

2016-01-18 00:45:38

package com.dd.model.protocol;


public class TTLDecoder
{
private TTL ttl;
private DataTypeEnum type;
private short len;
String serialData;
byte serialByte[];


public TTLDecoder(byte serialByte[])
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < serialByte.length; i++)
{
sb.append(String.format("%02x", serialByte[i]));
}
this.serialByte = serialByte;
this.serialData = sb.toString();
}
public TTL doDecoder()
{
TTL ttl = new TTL();

short tag = (short)Integer.parseInt(serialData.substring(0,4), 16);
short type = (short)Integer.parseInt(serialData.substring(4,8), 16);
short len = (short)Integer.parseInt(serialData.substring(8,12), 16);

DataTypeEnum enType = DataTypeEnum.values()[type];
ttl.setTag(tag);
ttl.setType(enType);
ttl.setLen(len);
return ttl;
}
private short getLenByType(DataTypeEnum type)
{
short byteWidth = 0;
switch (type)
{
case BOOL:
byteWidth = 1;
break;
case INT8:
byteWidth = 1;
break;
case INT16:
byteWidth = 2;
break;
case INT32:
byteWidth = 4;
break;
case FLOAT:
byteWidth = 4;
break;


default:
break;
}
return byteWidth;
}


}

阅读(1467) | 评论(0) | 转发(0) |
0

上一篇:TTL

下一篇:TypeLentMap

给主人留下些什么吧!~~