1.字节数组取反
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static byte[] backByte(byte[] buff)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i=0;i<buff.length;i++)
{
int b=0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j=0;j<8;j++)
{
int bit = (buff[i]>>j&1)==0?1:0;
b += (1<<j)*bit;
}
buff[i]=(byte)b;
}
return buff;
} 2.查找字节数组中字数组的位置
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static int indexOf(byte[] src,int offset,byte[] needFind)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=offset;i<src.length-offset-needFind.length;i++)
{
boolean isValid=true;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int j=0;j<needFind.length;j++)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(src[i+j]!=needFind[j])
{
isValid=false;
break;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(isValid)
{
return i;
}
}
return -1;
}3.字节数组转换为16进制
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private static final byte[] HEX_CHAR_TABLE =
{ (byte) '0', (byte) '1',
(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
(byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static String getHexString(byte[] raw, int len)
{
byte[] hex = new byte[2 * len];
int index = 0;
int pos = 0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (byte b : raw)
{
if (pos >= len)
break;
pos++;
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
![](/Images/OutliningIndicators/InBlock.gif)
return new String(hex);
}
posted on 2013-12-25 11:32
Terry Zou 阅读(698)
评论(0) 编辑 收藏 所属分类:
Tomcat+Eclipse