Results 1 to 8 of 8
  1. #1
    kriogenic is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default [Solved]Reading certain Address locations from file.

    Hey Everyone,

    I've run into a spot of trouble... I am developing an application in java and I need to open an executable file, starting at memory location 0x000081C0 read and convert the next 24 hex digits to readable ASCII format and store this new string into a variable...

    Can anyone help me with this, would be much appreciated.

    Thanks,
    Kriogenic.
    Last edited by kriogenic; 11-01-2010 at 06:18 PM. Reason: Problem Solved

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Your lack of code leaves me with no
    impression of the depth of your need
    for help.

    There is a simple, single line of
    code that will start you at the nth
    byte within an open file.

    I have none of my good references for
    this at hand right now, and I hate to
    make it sound this simple, but you
    should consult some of the methods in
    Java's file access APIs.

    Otherwise, I'm not sure of how big of
    a spot of help you are asking for.

  3. #3
    kriogenic is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I really have no code at the moment which works with the contents of the file...

    But it is a SNES rom file and I wish to output the rom name which is located at the address stated...

    I know how to read and write to an ascii text file for example but i have no idea where to start with working with a binary file and reading -> converting certain bytes...

    I am also unsure of which doc's would be benificial to read on doing so and my google searches were giving me little results as i did not know really what i was looking for...

    EDIT: What I have seen is reading into a byteArray, is this what i should be looking at?
    EDIT2: I found another source here CODE - which uses data input stream, I think this could work with what I want?
    Problem being I am unsure how to convert this to start at nth byte - THIS DIDNT WORK IT MADE JAVA CRASH when reading the rom file....

    Any info on which docs to read up or what i should be searching for to find my answer would be helpful,
    Thanks.
    Last edited by kriogenic; 10-31-2010 at 06:59 AM.

  4. #4
    kriogenic is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Hey I managed to get it working using the following
    Java Code:
    public static void ReadRomName(File file){
    	   try{
      //Create an array with a specified length
       byte[] fileBArray = new byte[(int)file.length()];
      //Open the file and read its bytes into a file
    FileInputStream fis = new FileInputStream(file);
    fis.read(fileBArray);
    
    //Go through the bytes in the file and transform them into a single string.... (starting at 33216 offset) spanning 24 characters.
    System.out.println(fileBArray.length);
    String finished = "";
    for(int k = 0; k < 24; k++){
    char value = (char)fileBArray[33216 + k];
    finished = finished + value;
    System.out.println(finished);
    }
    
    //Give an integer with the first occurance of a double space (should be end of rom name) - and substring accordingly)
    int pos = finished.indexOf("  ");
    finished = finished.substring(0,pos);
    System.out.println("Rom Name: " + finished);
    }catch(final Exception e){
    e.printStackTrace();
    }
    	}
    it loads it into a byte array, loops through a certain part of the array creates a string of up to 24 characters and strip away anything after and including double space, essentially leaving me with the rom name...

    Is this the best way to do this or is there a more optimized way?

    Thanks for all the help.

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Find nth location in a file

    Your solution is similar to one of several that
    could be used. I started to work on something like
    that, then I looked up some string methods in the
    API and decided to modify another application I
    already have in my files.

    The following is a complete working java application.
    I found it on the internet, and do not want to take
    credit for its design, which I think is very well
    organized and easy to analyze.

    Normally it can be used to open any file, allow some
    elementary editing of its contents, then save it as
    another file.

    By adding the following variables and one line of
    code..


    Java Code:
      private int nthValue = 32;
      private int length = 5;
      private String embeddedString;
    
      embeddedString = new String(b, nthValue, length);

    ..I modified this application to extract 5 bytes
    starting from location 32 of the file.

    I hope this is enough of an example to give you
    more ideas on how to accomplish your goal.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class RandomAccessDemo extends JFrame implements ActionListener{
      private JMenuItem jmiOpen;
      private JMenuItem jmiSave;
      private JMenuItem jmiExit;
      private JMenuItem jmiAbout;
      private JTextArea jta = new JTextArea();
      private JLabel jlblStatus = new JLabel();
      private JFileChooser jFileChooser = new JFileChooser();
      private int nthValue = 32;
      private int length = 5;
      private String embeddedString;
    
      public static void main(String [] args){
        RandomAccessDemo frame = new RandomAccessDemo();
        frame.setLocation(100, 200);
        frame.setTitle("Test JFileChooser");
        frame.setSize(300,150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    
      public RandomAccessDemo(){
        JMenuBar mb = new JMenuBar();
        setJMenuBar(mb);
    
        JMenu fileMenu = new JMenu("File");
        mb.add(fileMenu);
    
        JMenu helpMenu = new JMenu("Help");
        mb.add(helpMenu);
    
        fileMenu.add(jmiOpen = new JMenuItem("Open"));
        fileMenu.add(jmiSave = new JMenuItem("Save"));
        fileMenu.addSeparator();
        fileMenu.add(jmiExit = new JMenuItem("Exit"));
        helpMenu.add(jmiAbout = new JMenuItem("About"));
    
        jFileChooser.setCurrentDirectory(new File("."));
    
        getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
        getContentPane().add(jlblStatus, BorderLayout.SOUTH);
    
        jmiOpen.addActionListener(this);
        jmiSave.addActionListener(this);
        jmiExit.addActionListener(this);
        jmiAbout.addActionListener(this);
      }
      
    
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
    
        if (e.getSource() instanceof JMenuItem){
          if ("Open" .equals(actionCommand)){
            open();
          }
          else if ("Save" .equals(actionCommand)){
            save();
          }
          else if ("About" .equals(actionCommand)){
            JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs",
              "About this Demo", JOptionPane.INFORMATION_MESSAGE);
          }
          else if ("Exit" .equals(actionCommand)){
            System.exit(0);
          }
        }
      }
    
      private void open(){
        if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
          open(jFileChooser.getSelectedFile());
        }
      }
    
      private void open(File file){
        try{
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
          byte [] b = new byte[in.available()];
          in.read(b, 0, b.length);
          jta.append(new String(b, 0, b.length));
          in.close();
    
          embeddedString = new String(b, nthValue, 5);
          jlblStatus.setText("\"" + embeddedString + "\"  extracted from location  " + nthValue);
        }
        
        catch(IOException ex){
          jlblStatus.setText("Error opening file " + file.getName());
        }
      }
    
      private void save(){
        if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
          save(jFileChooser.getSelectedFile());
        }
      }
    
      private void save(File file){
        try{
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
          byte []  b = (jta.getText()).getBytes();
          out.write(b, 0, b.length);
          out.close();
    
          jlblStatus.setText(file.getName() + " saved ");
        }
    
        catch (IOException ex){
          jlblStatus.setText("Error saving " + file.getName());
        }
      }
    }

  6. #6
    kriogenic is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Hey thanks alot for that example, I have chosen to go with the method you have posted above and this is what I have adapted and it works much quicker and is alot neater then my previous way.

    Java Code:
    private String ReadName(File file){
        try{
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
          byte [] b = new byte[in.available()];
          in.read(b, 0, b.length);
          in.close();
    
          RomName = new String(b, 33216, 50);
    	  RomName = RomName.substring(0,RomName.indexOf("  "));
    	  return RomName;
        }
        
        catch(IOException ex){
         System.out.println("Error Opening File");
    	 return null;
        }
      }
    and it works great, thanks for your example it really helped. I looked up .read() and the string class and now I understand how it works.

    -Thanks,
    Kriogenic.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by kriogenic View Post
    I looked up .read() and the string class and now I understand how it works.

    -Thanks,
    Kriogenic.
    The API is your friend...:)

  8. #8
    kriogenic is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Indeed it is, I just didn't know which API sections to look up on like the string params etc. But I now do and its now all comming together great

Similar Threads

  1. Apache Velocity template file and output locations
    By mjwoodford in forum New To Java
    Replies: 1
    Last Post: 10-05-2009, 03:59 PM
  2. Problem with file address
    By ondra in forum New To Java
    Replies: 11
    Last Post: 08-06-2009, 11:13 AM
  3. Referencing Image Locations within Eclipse
    By haroldjclements in forum Eclipse
    Replies: 0
    Last Post: 03-23-2009, 02:34 PM
  4. Setting all locations at once in 2D arrays
    By Singing Boyo in forum New To Java
    Replies: 14
    Last Post: 03-22-2009, 10:49 PM
  5. Object locations via grid coordinates HELP.
    By deadman_uk in forum New To Java
    Replies: 4
    Last Post: 11-18-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •