package com.dd.model.protocol;
public class SerialDataDecoder
{
public static byte toHalfByte(String str)
{
byte result = 0;
String upperCaseString = null;
if(str.length()==1)
{
upperCaseString = str.toUpperCase();
if((upperCaseString.charAt(0) >= '0') && (upperCaseString.charAt(0) <= '9'))
{
result = (byte)(upperCaseString.charAt(0) - '0');
}
else if((upperCaseString.charAt(0) >= 'A') && (upperCaseString.charAt(0) <= 'F'))
{
result = (byte)(upperCaseString.charAt(0) - 'A' + 10);
}
else
{
result = 0;
}
}
return result;
}
public static byte toByte(String str)
{
byte result = 0;
byte hi;
byte low;
if(str.length()==2)
{
hi = toHalfByte(str.substring(0, 1));
low = toHalfByte(str.substring(1, 2));
result = (byte)((hi<<4) + low);
}
return result;
}
public static byte [] toByteArray(String str)
{
byte result [] = null;
if((str.length()%2) == 0)
{
int len = str.length();
result = new byte[len/2];
for (int i = 0; i < len; i+=2)
{
result[i/2] = toByte(str.substring(i,i+2));
}
}
return result;
}
}
阅读(1089) | 评论(0) | 转发(0) |