Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1483209
  • 博文数量: 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:47:18

package com.dd.model.protocol;


import java.nio.ByteBuffer;


public class ValueDecoder
{
private String serialData;
private DataTypeEnum type;
private byte []serialByte;


public ValueDecoder(DataTypeEnum type,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();
this.type = type;
}
public Value doDecoder()
{
Value result = null;
if (type == DataTypeEnum.BOOL)
{
Boolean t = stringToBoolean(serialData);
result = new Value<>(t);

} else if (type == DataTypeEnum.INT8)
{
Character t = stringToChar(serialData);
result = new Value<>(t);


} else if (type == DataTypeEnum.INT16)
{


Short t = stringToShort(serialData);
result = new Value<>(t);


} else if (type == DataTypeEnum.INT32)
{
Integer t = stringToInt(serialData);
result = new Value<>(t);


} else if (type == DataTypeEnum.FLOAT)
{
Float t = stringToFloat(serialData);
result = new Value<>(t);
} else if (type == DataTypeEnum.STRING)
{
result = new Value(serialData);
}
return result;
}
private Boolean stringToBoolean(String string)
{
Boolean result = null;
if(string.length()==2)
{
if((string.charAt(0) != 0) ||((string.charAt(1) != 0)))result = new Boolean(true);
else result = new Boolean(false);
}
return result;
}
private Character stringToChar(String string)
{
Character result = null;
if(string.length()==2)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getChar();
//result = new Character((char)t[0]);
}
return result;
}
private Short stringToShort(String string)
{
Short result = null;
if(string.length()==4)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getShort();
//result = new Short((short) ((t[0]<<8) + t[1]));
}
return result;
}
private Integer stringToInt(String string)
{
Integer result = null;
if(string.length()==8)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getInt();
//result = new Integer((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
}
return result;
}
private Float stringToFloat(String string)
{
Float result = null;
if(string.length()==8)
{
byte t[] = stringToByte(string);
ByteBuffer buf = ByteBuffer.allocate(t.length);
buf.put(t);
buf.flip();
//int rawIntBit = ((int) ((t[0]<<24) +(t[1]<<16) +(t[2]<<8) + t[3]));
//result = new Float(Float.intBitsToFloat(rawIntBit));
result = buf.getFloat();
}


return result;
}

private byte[] stringToByte(String string)
{
if((string.length() %2) != 0)return null;
ByteBuffer buf = ByteBuffer.allocate(string.length());
int len = string.length() /2;
byte []result = new byte[len];

for (int i = 0; i < result.length; i++)
{
char hi = string.charAt(2*i);
char low = string.charAt(2*i+1);
int c = 0;
if(Character.isDigit(hi)) c |= hi - '0';
else if(Character.isUpperCase(hi))c |= hi - 'A' + 10;
else if(Character.isLowerCase(hi))c |= hi - 'a'+ 10;

c =  (c  * 16);


if(Character.isDigit(low)) c |= low - '0';
else if(Character.isUpperCase(low))c |= low - 'A' + 10;
else if(Character.isLowerCase(low))c |= low - 'a' + 10;


result[i] = (byte)c;
}
return result;
}
}

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

上一篇:Value

下一篇:利用qemu进行内核源码级调试

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