跪求二进制文件转十六进制java源代码,我都搜了好多方法了转出的结果都和我需要的结果不同。谢谢了

解决方案 »

  1.   

    问题可以描述清楚点吗?二进制/十六进制,是文字串形式还是数值形式?
    方法入口出口类型没说清楚,这个给段文字串类型的
    public class Test {
        public static void main(String[] args) throws Throwable {
            String bs = "10101111000100";
            System.out.println(toHexString(bs));
        }    public static String toHexString(String binaryString) {
            StringBuilder buf = new StringBuilder();
            for (int i=binaryString.length(); i>=0; i-=4) {
                buf.insert(0, String.format("%X", Integer.valueOf(binaryString.substring(Math.max(0,i-4), i), 2)));
            }
            return buf.toString();
        }
    }
      

  2.   

    能举个例子吗,比如源文件是什么样的,目标文件是什么样的?
    现在不知道你是要以read(byte[])方式把每一个byte内码转成16进制保存,还是以readLine的方式把二进制字符串内容转成16进制
      

  3.   

    public static String encode(String str, String encode)
    throws UnsupportedEncodingException {
    byte[] bytes = str.getBytes(encode); StringBuilder sb = new StringBuilder();
    int len = bytes.length;
    for (int i = 0; i < len; i++) {
    byte b = bytes[i];
    int b01 = (b & 0xf0) >> 4;
    sb.append(getChar(b01));
    int b02 = b & 0x0f;
    sb.append(getChar(b02));
    if (i != (len - 1)) {
    sb.append(' ');
    }
    }
    return sb.toString();
    } public static char getChar(int b) {
    if (b >= 0 && b <= 9) {
    return (char) ('0' + b);
    } if (b >= 10 && b <= 15) {
    return (char) ('A' - 10 + b);
    } throw new IllegalArgumentException("error byte:" + b);
    }
      

  4.   

    public static String encode(String str, String encode)
    throws UnsupportedEncodingException {
    byte[] bytes = str.getBytes(encode); StringBuilder sb = new StringBuilder();
    int len = bytes.length;
    for (int i = 0; i < len; i++) {
    byte b = bytes[i];
    int b01 = (b & 0xf0) >> 4;
    sb.append(getChar(b01));
    int b02 = b & 0x0f;
    sb.append(getChar(b02));
    if (i != (len - 1)) {
    sb.append(' ');
    }
    }
    return sb.toString();
    } public static char getChar(int b) {
    if (b >= 0 && b <= 9) {
    return (char) ('0' + b);
    } if (b >= 10 && b <= 15) {
    return (char) ('A' - 10 + b);
    } throw new IllegalArgumentException("error byte:" + b);
    }
      

  5.   

    package test;
     
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
     
    public class ReadBinaryTest {
     public void testDataInputStream(String filepath) throws IOException {
      File file = new File(filepath);
      DataInputStream din = new DataInputStream(new FileInputStream(file));
      StringBuilder hexData = new StringBuilder();
      StringBuilder asciiData = new StringBuilder();
      byte temp = 0;
      for(int i=0;i<file.length();i++) {
       temp = din.readByte();
       // 以十六进制的无符号整数形式返回一个字符串表示形式。
       String str = Integer.toHexString(temp);
    //   System.out.println(++i +":" + Integer.toHexString(temp).length() + ":" + Integer.toHexString(temp));
    //   System.out.flush();
       if(str.length() == 8) {//去掉补位的f
        str = str.substring(6);
       }
       if(str.length() == 1) {
        str = "0"+str;
       }
       hexData.append(str.toUpperCase());
       //转换成ascii码
       String ascii = "";
       if(temp>34 && temp <127) {
        char c = (char)temp;
        ascii = c+"";
       }else{
        ascii = ".";
       }
       asciiData.append(ascii);
       //System.out.println(str.toUpperCase()+"|"+temp+"|"+ascii);
      }
      //计算行数
      int line = hexData.toString().length()/32;
      din.close();
      StringBuilder lines = new StringBuilder();
      for(int i=0;i<=line;i++) {
       String li = i+"0";
       if(li.length() == 2){
        li = "00"+li;
       }
       if(li.length() == 3) {
        li = "0"+li;
       }
       lines.append(li);
      }
      System.out.println(lines.toString());
     }
     public static void main(String[] args) throws IOException {
      String filepath = "C:\\Users\\s\\Desktop\\erb1_pcap\\erb1.pcap";
      ReadBinaryTest test = new ReadBinaryTest();
      test.testDataInputStream(filepath);
     }
    }
    erb1是对应的二进制文件 用UltraEdit打开即可看到要转换到十六进制的效果!!!!
      

  6.   


    /**
     * 普通文件转换为16进制文件
     * 
     * @param srcFilePath
     *            源文件
     * @param dstFilePath
     *            目标文件
     * @throws IOException
     *             IOException
     */
    public static void fileToHex(String srcFilePath, String dstFilePath)
    throws IOException {
    File srcFile = new File(srcFilePath);
    File dstFile = new File(dstFilePath);
    File parentFile = dstFile.getParentFile(); // 存放目标文件的文件夹不存在,则新建
    if (!parentFile.exists()) {
    if (!parentFile.mkdirs()) {
    throw new IOException("File Creat Failed.");
    }
    } FileInputStream in = null;
    try {
    in = new FileInputStream(srcFile); FileOutputStream out = null;
    try {
    out = new FileOutputStream(dstFile);
    byte[] buffer = new byte[BUFFER_SIZE];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
    out.write(byteToHex(buffer, 0, len).getBytes());
    out.flush();
    }
    } finally {
    if (out != null) {
    out.close();
    out = null;
    }
    }
    } finally {
    if (in != null) {
    in.close();
    in = null;
    }
    }
    } /**
     * byte数组转换为十六进制字符串
     * 
     * @param bytes
     *            byte数组
     * @param len
     *            转换长度
     * @return 转换后十六进制字符串
     */
    public static String byteToHex(byte[] bytes, int off, int len) {
    StringBuilder sb = new StringBuilder(len * 3);
    for (int i = off; i < len; i++) {
    byte b = bytes[i];
    int b01 = (b & 0xf0) >> 4;
    sb.append(getChar(b01));
    int b02 = b & 0x0f;
    sb.append(getChar(b02));
    sb.append(' ');
    }
    return sb.toString();
    } /**
     * byte转换为十六进制字符串
     * 
     * @param b
     *            byte
     * @return 转换后十六进制字符串
     */
    public static String byteToHex(byte b) {
    char b01 = getChar((b & 0xf0) >> 4);
    char b02 = getChar(b & 0x0f);
    return String.valueOf(b01) + String.valueOf(b02);
    } /**
     * 取得16进制字符串
     * 
     * @param b
     *            16进制的数字表示
     * @return 16进制的char
     * @throws IllegalArgumentException
     *             如果非16进制的数字则抛出该异常
     */
    public static char getChar(int b) {
    if (b >= 0 && b <= 9) {
    return (char) ('0' + b);
    } if (b >= 10 && b <= 15) {
    return (char) ('A' - 10 + b);
    } throw new IllegalArgumentException("error byte:" + b);
    }