1.字节数组取反
public static byte[] backByte(byte[] buff){
for (int i=0;i<buff.length;i++){
int b=0;
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.查找字节数组中字数组的位置
public static int indexOf(byte[] src,int offset,byte[] needFind){
for(int i=offset;i<src.length-offset-needFind.length;i++){
boolean isValid=true;
for(int j=0;j<needFind.length;j++){
if(src[i+j]!=needFind[j]){
isValid=false;
break;
}
}
if(isValid){
return i;
}
}
return -1;
} 3.字节数组转换为16进制
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' };
public static String getHexString(byte[] raw, int len) {
byte[] hex = new byte[2 * len];
int index = 0;
int pos = 0;
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];
}
return new String(hex);
}
posted on 2013-12-25 11:32
Terry Zou 阅读(706)
评论(0) 编辑 收藏 所属分类:
Tomcat+Eclipse