Welcome to Yumao′s Blog.
將任意文件轉換成Hex(十六進制)文本
, 2014年04月19日 , Java Language , 评论 在〈將任意文件轉換成Hex(十六進制)文本〉中留言功能已關閉 ,

想必有時候遇到比較煩惱的事情
就是知道用十六進制查看文檔
不過又沒有順手的hex查看器
那麽就自己動手寫一個程式
將文件轉換成十六進制文本吧~

直接給出代碼
有點基礎的應該都可以看得懂喔
即使看不懂直接複製黏貼運行也可耶~XD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
 
public class HexFinder {
    public static String format(byte[] bt) {
        int line = 0;
        StringBuilder buf = new StringBuilder();
        for (byte d : bt) {
            if (line % 16 == 0)
            buf.append(String.format("%06x : ", line));
            buf.append(String.format("%02x ", d));
            line++;
            if(line%16==0){
                buf.append("\n");
              }
        }
        buf.append("\n");
        return buf.toString();
    }
    public static byte[] readFile(String file) throws IOException {
        InputStream is = new FileInputStream(file);
        int length = is.available();
        byte bt[] = new byte[length];
        is.read(bt);
        return bt;
    }
 
    public static void main(String[] agrs) throws IOException {
        byte[] bt = HexFinder.readFile("0a0000.win32.dat0");
        String hexData = HexFinder.format(bt);
        String filePatch = "0a0000.win32.dat0.hex";
        FileWriter fileWriter = new FileWriter(filePatch);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(hexData);
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
    }
}
关键字:, , ,

评论已关闭