0

I need to do this project where I take a binary file read from it, and then make a new binary file that has adds 00 depending on the offset. So for example, if the binary number is at offset 02, it will shift that binary number by 2. So that binary number will be at 04. with 00 in between.

I am thinking of using the Random Access File to access it and edit (read only). But I am lost at by how many bytes do you seek. Also, how do edit and put the modification into a brand new file.

Kindest Regards

This is the hex format of the binary file

King A
  • 37
  • 10
  • Please be more precise with your description: are you trying shift content by adding the _byte value_ 0x00 at specific byte offsets? If so, I'm not sure where the problem is. You read in the binary data, and then if your data structure of choice allows element insertion, do that. Otherwise you can "insert" an element at whatever `n` you need by creating a new list of values `list[:n] + (new value) + list[n+1:]` – Mike 'Pomax' Kamermans Nov 25 '19 at 20:15
  • @Mike'Pomax'Kamermans thank you for your reply. I am doing this in java, I just created a random access file but I do not know by how much should I seek it using the .seek(n) method. I am confused on that. Sorry, I am a beginner at this. – King A Nov 25 '19 at 20:22
  • @Mike'Pomax'Kamermans I am not familiar with these Hex formats – King A Nov 25 '19 at 20:24
  • @Mike'Pomax'Kamermans your example of list is in Python but the tag is java. – WJS Nov 25 '19 at 20:30
  • 2
    @KingA To do what you want you need to read in one file and modify it as you write it out to another file. Based on your description, if you had 10 bytes to insert somewhere, you would read to the insertion point and write out those values. Then write out the 10 bytes and continue on in the same fashion of reading, and writing. – WJS Nov 25 '19 at 20:32
  • @WJS thanks a lot, my only question is by how much do i seek? I have to do this for every pair in the picture provided above. However, I'm unsure of what value I can put in the seek function. I already have a loop that reads until EOF. thanks – King A Nov 25 '19 at 20:56
  • My example wasn't even in Python, as a comment rather than an answer, it was an illustration of the idea of inserting by splitting, in no language in particular. As for hex format: 0x is the universal prefix for a hexadecimal number. If you say 15, that's universally understood as decimal, whereas 0x15 is understood as the hex number with decimal equivalent 21 (to the point where it's considered a standard notation in most programming languages, java included. `int x = 0x15` will give you an int called `x` with decimal value `21` in it) – Mike 'Pomax' Kamermans Nov 25 '19 at 22:37
  • @Mike'Pomax'Kamermans My mistake. – WJS Nov 26 '19 at 20:17

1 Answers1

0

enter image description here

In the image above. the first byte in blue (0x54) is at 0x40 + 0x0E. Add the left row value plus the column value. So you must read everthing up to but excluding that byte. So that would be offset 0x40 + 0x0D. That would be 64 + 13 = 77. Since the offset starts at 0, read in the first 78 bytes then write out what you want. Then write out the rest of the file. That means all the blue would be written after you special insertion.

This is all presuming that, that is what you want to do. But you can't really use seek because that would imply you are modifying the source file which is not wise (since you might make a mistake and need to start over). Also seeking into a file under modification can be very problematic since its size may keep changing. In any event make certain you keep a backup copy of the file.

To make further modifications, keep reading and writing as described.

Here is a brute force example. It could be improved with try-with-resources and loops as appropriate. Exceptions are presumed to be monitored. This example simply inserts two triads of integers in specified locations and them prints the new file.

      // create a file to start
      FileOutputStream fo = new FileOutputStream("myfile.txt");
      String alpha = "abcdefghijklmnopqrstuvwxyz";
      fo.write(alpha.getBytes());
      fo.close();

      // create the new output file and open up the old one as an input
      // file
      fo = new FileOutputStream("myfile2.txt");
      FileInputStream fi = new FileInputStream("myfile.txt");
      // insert 123 after k
      byte[] buf = new byte[100];
      int count = fi.read(buf, 0, 11); // read up to and including k
      fo.write(buf, 0, count);
      fo.write("123".getBytes());
      // insert 456 after t
      count = fi.read(buf, 0, 9);
      fo.write(buf, 0, count);
      fo.write("456".getBytes());
      //copy rest of input file to output file
      while ((count = fi.read(buf, 0, 10)) > 0) {
         fo.write(buf, 0, count);
      }
      fo.close();
      fi.close();

      fi = new FileInputStream("myfile2.txt");
      byte[] buffer = fi.readAllBytes();
      for (byte b : buffer) {
         System.out.print((char) b);
      }
      System.out.println();
      fi.close();
   }
WJS
  • 36,363
  • 4
  • 24
  • 39