By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Reading a JPEG's Binary Code

P: 3
Hello! I am new to the whole programming world (enjoying it). I wanted to work with jpeg files. I read in a jpeg file using the open method with "rb" which it all works fine. Then I wrote all its binary code into a text file to analyze.

The text file looks a lot like hex-decimal. The thing I do not understand is, how come there are characters like "?" and "(" mixed in the code.? I have also seen 00Z mixed in. Shouldn't it all be from 0 to F only?

I noticed these symbols do not appear if I open the file with HxD. In fact, some of the Hex code is different.
Oct 22 '14 #1
Share this Question
Share on Google+
3 Replies


P: 3
When I open with HxD, it has a particular Hex number as 00 5A 00 5A. In python, where that piece of code should be; I get 00z00z. I am confused by that.
Oct 22 '14 #2

Rabbit
Expert Mod 10K+
P: 12,349
Binary does not equal hex. You have to convert the binary to hex representation before writing it to the file.

In binary, the value could be something like 01011010. Which in decimal is 90, in hex it's 5A. All those values: 01011010, 90, and 5A are merely different visual representations of the underlying binary data. How the binary data is represented is up to the program. For example, the text editor can read in the binary 01011010 and convert that into it's corresponding ASCII value of Z and display that to you. Whereas if you open the same file in an image editor, it will take the same value and convert it to a color.

If you want to take the binary data and change the way a text editor displays the data, then you need to make that conversion. If all you do is read in binary data and write out the same binary data, all you're doing is making a copy of the file.
Oct 22 '14 #3

P: 3
"For example, the text editor can read in the binary 01011010 and convert that into it's corresponding ASCII value of Z"

You hit the nail there. Thanks. I'll keep playing with this.
Oct 22 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.