十六进制表文件

问题描述 投票:0回答:1

我需要将我的文件数据转换为十六进制,并将其填充到十六进制表格中,并且已经使用了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  |  
java hex number-formatting
1个回答
0
投票

最后,我终于找到了一个如何填充我的表格的方法。

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");
        }

      }

}
© www.soinside.com 2019 - 2024. All rights reserved.