Java read hex file

As part of a larger program, I need to read the values ​​from a hex file and print the decimal values. It seems to be working fine; However, all hexadecimal values ​​from 80 to 9f give incorrect values. for example, 80 hex gives the decimal value of 8364. Please help.

this is my code:

String filename = "pidno5.txt"; FileInputStream ist = new FileInputStream("sb3os2tm1r01897.032"); BufferedReader istream = new BufferedReader(new InputStreamReader(ist)); int b[]=new int[160]; for(int i=0;i<160;i++) b[i]=istream.read(); for(int i=0;i<160;i++) System.out.print((b[i])+" "); 
+4
source share
2 answers

If you tried to read raw bytes, this is not what you are doing.

You are using a Reader that reads characters (in an encoding you did not specify, so it defaults to something, possibly UTF-8).

To read bytes, use an InputStream (and don't wrap it in Reader).

+7
source

You can also use a different encoding:

 BufferedReader istream = new BufferedReader(new InputStreamReader(ist, "ISO-8859-15")); 
0
source

All Articles