[java] Problems reading bytes from a file

Started by
4 comments, last by GameDev.net 18 years, 1 month ago
Hi, this might sound as a newbie question but the truth is I'm actually quite experienced with Java and surprised at the problem I have. I simply want to open a file, read its contents into a string, and make a SHA1 hash of the string. For some stupid reason, after reading the file, all occurences of bytes '8f' (hex code) have been replaced by '3f'. Therefore the hash is totally wrong... Does anybody know a quick fix for this? Thanks, Bas
Advertisement
If the file was produced by a source other than a standard Java output stream, perhaps you are running afoul of UTF encoding (Linkie). I must admit I don't have much experience with that except for the vague memory that you could have some potential problems with that.
Do you create this file through a Java application? If not, endianness might be the problem, though I don't know if endianness counts for the issue at hand.

How do you open this file? I suggest you use Java NIO (ByteBuffer, Charset) to encode/decode this data according to what you're expecting from it. That is, if you're not already using NIO =D

Son Of Cain
a.k.a javabeats at yahoo.ca
Thanks for the help. I guess there just isn't a way to do it, so I've simply rewritten my program to use a byte array.
Not knowing how you're trying to read what kind of file noone can really help you...

Most common mistake I see when people have trouble reading and writing files using Java has to do with them attempting to use Writers instead of Streams to operate on binary data.
int uint8 = inputStream.read() & 0xff


This topic is closed to new replies.

Advertisement