gpt4 book ai didi

java - 文件存入十六进制表

转载 作者:行者123 更新时间:2023-12-01 17:17:42 24 4
gpt4 key购买 nike

我需要将文件数据转换为十六进制,并用十六进制表填充它。并且使用了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");
}

}

}

关于java - 文件存入十六进制表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61351268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com