Welcome to Yumao′s Blog.

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

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

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();
	}
}
关键字:, , ,

评论已关闭