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;
}
}
阅读(1571) | 评论(0) | 转发(0) |