4

As part of a larger program, I need to read values from a hex file and print the decimal values. It seems to be working fine; However all hex values ranging from 80 to 9f are giving wrong values. for example 80 hex gives a 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])+" ");
1
  • 1
    Just for terminology: You are not reading a hex file (which would be a file containing hexadecimal data coded in ASCII?), but a binary file. Commented Jun 14, 2011 at 11:58

2 Answers 2

7

If you were trying to read raw bytes this is not what you are doing.

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

To read bytes, use an InputStream (and do not wrap it in a Reader).

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

1 Comment

The default encoding is probably some Microsoft code page, as some of them have the € sign on code point 0x80, which is then mapped to Unicode 8364.
0

You may also use a different encoding:

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

Comments

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.