I need to convert my file data into hex, and populating it with hex table.and have used seek() you can call read() and 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 + " | ");
}
}
}
am trying to populate with data, am already create table but how to populate hex data
here is my output
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 |
finally, I found a way how to populate my table
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");
}
}
}