哪路大神可以给个PDF转二进制字符串的例子,不要网上随便找的,谢谢
顺手给你写一个,不谢
public static byte[] getFileBytes(String 路径){
ArrayList bytesList = new ArrayList();
byte[] bytes = new byte[1];
FileInputStream inStream = null;
try{
inStream = new FileInputStream(路径);
while(inStream.read(__1bytes)>0){
bytesList.add(__1bytes[0]);
}
bytes = new byte[bytesList.size()];
for(int i;i<bytes.length;i++){
bytes[i] = bytesList.get(i);
}
return bytes;
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
try{
if(inStream!=null){
inStream.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
你不要16进制的么,接着写,精彩来了,效率和可读性平衡一下,效率再高代码就多了不好维护,自己看,你要字符串就把得到的byte[]给toString一下,你要写到文件里就直接write字节
byte[] getHexBytes(byte[] bytes){
byte[] newBytes = new byte[4*bytes.length]
byte[] 0xbytes = new byte[]{(int)'0',(int)'x',(int)'0',(int)'0'};
for(int i=0;i<bytes.length;i++){
System.arraycopy(0xbytes,0,newBytes,i*4,4);
char[] hexChars = Integer.toHexString((int)bytes[i]).toCharArray();
for(int j=0;j<hexChars.length;j++){
newBytes[i*4+4-hexChars.length+j] = (int)hexChars[j];
}
}
return newBytes;
}
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.println(String.format("0x%02x",tempbyte));
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}