posts - 93,  comments - 2,  trackbacks - 0
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 阅读(685) 评论(0)  编辑  收藏 所属分类: Tomcat+Eclipse

只有注册用户登录后才能发表评论。


网站导航:
 
<2013年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜