Home » Java » debugging – Java Rudimentary Hex Editor – Programming Assignment-Exceptionshub

debugging – Java Rudimentary Hex Editor – Programming Assignment-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I am coding a rudimentary hex editor for an assignment in school. I have it just about 90% done, however, I can’t figure out how to assign a new byte value to an already existing byte value. Basically, I ask the user if he/she would like to modify a byte at the position they specify (earlier in the code). If they choose yes, then they should be prompted to enter a new HEX value to overwrite the one at the position. So far, I have this:

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

Here, I can’t really figure out how to then put the value they enter into the corresponding position.

Here’s the previous code that sets the position the user specifies:

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


Any help at all is greatly appreciated. Thank you.

How to&Answers:

If I understand correctly your fileList contains every byte of the open file. That is how you retrieve the selected bytes at least.

So maybe you can use the set() method to change a byte of the fileList (https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#set(E))

I think you need to go to the correct offset, call next() to retrieve it and then call set() to define it if I understand the docs correctly.

After you perfromed this operation the byte is probabbly only altered in memory and you would still need to write them to disk, if you want to preserve them.