1

Okay guys, I have a file with some HEX values as well as a program that take this values with a byte[] in order to convert some hex values and then reconvert it to a file. The problem is that when I reconvert de byte array to a file some hex values are modified, and I don't find the problem. If you see any possible mistake don't hesitate.

As you can see I have a test.sav file, here it is:

And this is the product of the program, the two files are different and they should be the same because any change has been made:

Here is the code:

public class Test {

public static File file;
public static String hex;
public static byte[] mext;
public static byte[] bytearray;

public static void main(String[] args) throws IOException {
    file = new File("C:\\Users\\Roman\\Desktop\\test.sav");
    StringBuilder sb = new StringBuilder();
    FileInputStream fin = null;

    try {
        fin = new FileInputStream(file);
        bytearray = new byte[(int)file.length()];
        fin.read(bytearray);

        for(byte bytev : bytearray){ 
            sb.append(String.format("%02X", bytev));
        }
        System.out.println(sb);


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {}

    //replaceMax(); <-- I deduced that conversion is not the problem 
    save(); // THIS IS THE IMPORTANT PART

}


public static void save() throws IOException{
    PrintWriter pw = new PrintWriter("C:\\Users\\Roman\\Desktop\\test2.sav");
    pw.write("");
    pw.close();
    FileWriter fw = new FileWriter(new File("C:\\Users\\Roman\\Desktop\\test2.sav"));
    BufferedWriter out = new BufferedWriter(fw);
    out.write(new String(bytearray, "ASCII"));
    out.close();
}

} 
3
  • You are not using FileInputStream.read correctly. It might not return you all of the data in case I/O blocks. Check out the documentation. Commented Dec 4, 2014 at 23:26
  • How can I correct the I/O blocks, or just avoid them? Commented Dec 5, 2014 at 7:26
  • You should check the return value of read and if it is less than bytarray.length, call read again for the remaining bytes until finally the entire array is eventually read. You'll want to switch to using the overload of read that accepts an offset as additional parameter. Commented Dec 5, 2014 at 19:10

1 Answer 1

1

You are reading data from a binary file and then trying to write it out as a character stream. Furthermore you're forcing it to use ASCII (a 7 bit character set) as the character encoding.

Try altering the save method to use:

     FileOutputStream output = new FileOutputStream("C:\\Users\\Roman\\Desktop\\test2.sav");
     try {
         output.write(bytearray);
     } finally {
         output.close();
     }

This will avoid character (re)encoding issues.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I will try it this afternoon, if it works I'll set your answer as the correct one :)
Which Charset should I use? Can you explain the 7 bit characteristic of ASCII charset?
Steve's solution doesn't use any character set at all which is what you want. Instead, the bytes are treated as, well, bytes. Remember that there are only 2^7 = 128 ASCII characters defined but a byte can take on 2^8 = 256 different values so you cannot decode arbitrary byte arrays into sequences of ASCII characters in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.