java - 文件到十六进制表
问题描述
我需要将我的文件数据转换为十六进制,并用十六进制表填充它。并且使用了 seek() 你可以调用 read() 和 printf();
public class HexEditor {
static int sixteen = 16;
public static void main(String[] args) throws IOException {
File myFile = new File("a.dat");
RandomAccessFile raf = new RandomAccessFile(myFile, "rw");
printHexTable(raf, 0);
}
public static void printHexTable(RandomAccessFile accessFile, int rownumber) throws IOException {
accessFile.seek(rownumber * sixteen);
int readByte = accessFile.read();
System.out.printf("%02X", readByte);
System.out.print("\n");
System.out.print(" H |");
for (int i = 0; i < 16; i++) {
String str = Long.toHexString(i);
System.out.print(" " + str.toUpperCase() + " |");
}
System.out.print("\n");
System.out.print("-----|");
for (int i = 0; i < sixteen; i++) {
System.out.print("------");
}
System.out.print("\n");
for (int x = rownumber; x < sixteen; x++) {
String str = Long.toHexString(x);
System.out.println(" " + str + " | ");
}
}
}
我正在尝试填充数据,我已经创建了表但是如何填充十六进制数据
这是我的输出
H | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
-----|------------------------------------------------------------------------------------------------
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
a |
b |
c |
d |
e |
f |
解决方案
最后,我找到了一种方法来填充我的表格
public class HexEditor {
static int sixteen = 16;
public static void main(String[] args) throws IOException {
File myFile = new File("a.dat");
RandomAccessFile raf = new RandomAccessFile(myFile, "rw");
printHexTable(raf, 0);
}
public static void printHexTable(RandomAccessFile accessFile, int rownumber) throws IOException {
int readByte = 0;
int count = 0;
// System.out.printf("%02X", readByte);
System.out.print("\n");
System.out.print(" H |");
for (int i = 0; i < 16; i++) {
String str = Long.toHexString(i);
System.out.print(" " + str.toUpperCase() + " |");
}
System.out.print("\n");
System.out.print("-----|");
for (int i = 0; i < sixteen; i++) {
System.out.print("------");
}
System.out.print("\n");
accessFile.seek(rownumber * sixteen);
int bytesCounter = 0;
for (int x = rownumber; x < sixteen; x++) {
while ((readByte = accessFile.read()) != -1) {
if (bytesCounter < sixteen) {
if (count == 0) {
System.out.print(" " + Long.toHexString(count) + " |");
count++;
}
System.out.print(" ");
System.out.printf("%02X", readByte);
System.out.print(" ");
bytesCounter++;
} else {
System.out.print("\n");
System.out.print(" " + Long.toHexString(count) + " |");
bytesCounter = 0;
count++;
}
}
System.out.print("\n");
}
}
}
推荐阅读
- puppeteer - React-Select:如何为下拉列表的每个选项添加自定义类名?
- amazon-elastic-beanstalk - Amazon Route 53 域名,链接到 Elastic Beanstalk
- opencv - 图像数据生成器+自动标记+自动绘制基于对象分割的边界框
- grails - Grails 3.3.x 中的 SAML sp.xml 中的证书问题
- testing - 由于 Testcafe 前置请求 URL,对 API 端点的请求被拒绝 - 解决方案?
- python - 无法让 URL fstring 正常工作
- azure - 如何使用 Polyrepo 订购构建
- python - 按字母顺序排序篮球队字典
- angular - 使用 GitHub 使用 AWS Amplify 进行部署时无法承担指定的 IAM 角色
- iphone - 指定 xcresult 文件名