Jump to content
  • Advertisement
Sign in to follow this  
basananas

[java] Problems reading bytes from a file

This topic is 4517 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
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.

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
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.

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
Guest Anonymous Poster
int uint8 = inputStream.read() & 0xff


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!