首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java初级十六进制编辑器-编程赋值

Java初级十六进制编辑器-编程赋值
EN

Stack Overflow用户
提问于 2020-02-18 12:15:41
回答 1查看 26关注 0票数 1

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

代码语言:javascript
复制
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);

在这里,我真的不知道如何将它们输入的值放入相应的位置。

下面是设置用户指定位置的前面的代码:

代码语言:javascript
复制
        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++;

            }

任何帮助都是非常感谢的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-02-18 16:19:29

如果我没记错的话,你的fileList包含打开文件的每一个字节。这至少是您检索所选字节的方式。

因此,也许您可以使用set()方法来更改fileList的一个字节(https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#set(E)

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60273659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档