Java基本十六进制编辑器-编程任务

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

我正在为一名在校作业编写基本的十六进制编辑器。我大约完成了90%,但是,我不知道如何将新的字节值分配给已经存在的字节值。基本上,我问用户他/她是否想在他们指定的位置(代码的前面)修改一个字节。如果选择是,则应提示他们输入新的十六进制值以覆盖该位置的值。到目前为止,我有这个:

System.out.print("Would you like to edit the byte at this position?(y/n): ");
            String choice;
            choice = in.nextLine();
            if ("y".equals(choice)){
                System.out.print("Enter the new hex value: ");
                String hv = in.nextLine();
                int i = Integer.decode("0x" + hv);

在这里,我真的无法弄清楚如何将他们输入的值放在相应位置。

这是设置用户指定位置的先前代码:

        System.out.print("Enter byte positon to navigate to: ");
        long loc = in.nextLong(); in.nextLine();           
        if (loc >= 0 && loc < fileList.size()) {
            ListIterator<Byte> lit = fileList.listIterator((loc - 5 >= 0)?(int)(loc-5):0);
            int start = lit.nextIndex();
            System.out.println("The surrounding 5 bytes (left and right) are: ");
            while (start <= loc+5 && start < fileList.size()){
                if (start == loc){
                   System.out.print(" ( ");
                }
            byte b = lit.next();
            System.out.print(((b >= 0 && b <= 15)?"0":" ") + Integer.toHexString((int)b & 0x00FF) + " ");
            if (start == loc)
                System.out.print(" ) ");
            System.out.print(" ");
            start++;

            }

非常感谢任何帮助。谢谢。

java debugging hex byte
1个回答
0
投票

如果我理解正确,您的fileList包含打开文件的每个字节。这就是您至少检索所选字节的方式。

所以也许您可以使用set()方法来更改fileListhttps://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#set(E))的字节

我认为您需要转到正确的偏移量,如果我正确理解文档,请调用next()来检索它,然后调用set()来定义它。

执行完此操作后,字节可能仅在内存中被更改,如果要保留它们,则仍需要将其写入磁盘。

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