Reading a JPEG's Binary Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Velchivz
    New Member
    • Oct 2014
    • 3

    Reading a JPEG's Binary Code

    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.
  • Velchivz
    New Member
    • Oct 2014
    • 3

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

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

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

      Comment

      • Velchivz
        New Member
        • Oct 2014
        • 3

        #4
        "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.

        Comment

        Working...