Sign in to follow this  

[java] Problems reading bytes from a file

This topic is 4301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this