日期:2014-05-20 浏览次数:20901 次
public static void main(String[] args) throws IOException {
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(new File("D:\\123.txt")));
byte[] b = new byte[1024];
int len = 0;
while((len = bi.read(b)) != -1){
System.out.println(bytes2BinaryStr(Arrays.copyOf(b, len)));
}
bi.close();
}
public static String bytes2BinaryStr(byte[] bytes){
String hexStr = "0123456789ABCDEF";
String result = "";
String hex = "";
for(int i=0;i<bytes.length;i++){
//字节高4位
hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));
//字节低4位
hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));
result +=hex+" ";
}
return result;
}
/**
* 将字节数组中的所有字节转换成 16 进制字符串
*
* @param b byte[]
* @param len int
* @return String