← 回到文章列表

将任意文件转换成Hex(十六进制)文本

想必有时候遇到比较烦恼的事情 就是知道用十六进制查看文档 不过又没有顺手的hex查看器 那么就自己动手写一个程式 将文件转换成十六进制文本吧~ 直接给出代码 有点基础的应该都可以看得懂喔 即使看不懂直接复制黏贴运行也可耶~XD

想必有时候遇到比较烦恼的事情
就是知道用十六进制查看文档
不过又没有顺手的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();
	}
}

在这里找找

输入关键词,找一篇过去的记录。