How to check for alphanumeric characters in a binary file
<p>I'm a beginner trying to write a program that will read in .exe files, .class files, or .pyc files and get the percentage of alphanumeric characters (a-z,A-Z,0-9).  Here's what I have right now (I'm just trying to see if I can identify anything at the moment, not looking to count stuff yet):</p>

<pre><code>chars_total = 0
chars_alphnum = 0
iterate = 1

with open("pythonfile.pyc", "rb") as f:
    byte = f.read(iterate)
    while byte != b"":

        chars_total += 1
        print (byte)

        iterate +=1
        byte = f.read(iterate)
</code></pre>

<p>This code prints out various bytes such as </p>

<pre><code>b'\xe1WQ\x00'
b'\x00\x00c\x00\x00'
</code></pre>

<p>but I'm having trouble with translating the bytes themselves.</p>

<p>I've also tried <code>print (binascii.hexlify(byte))</code> after importing binascii which converts everything into alphanumeric characters, which seems to not quite be what I'm looking for.  So am I just getting something severely mistaken or am I at least on the right track?</p>

<p>Full disclaimer, this is related in small part to a homework assignment, but we have permission to use this site because neither the in class material nor the reading covers any coding at all.  And yes, I have been trying to figure this out before I came on here.</p>

<p>On Windows, you could use a simple PowerShell script to get the hexdump (take a look here <a href="http://windowsitpro.com/powershell/get-hex-dumps-files-powershell" rel="nofollow">http://windowsitpro.com/powershell/get-hex-dumps-files-powershell</a>) and then, decode it to whatever standard you want (ascii, unicode) with Python (take a look here <a href="https://docs.python.org/2/library/functions.html#chr" rel="nofollow">https://docs.python.org/2/library/functions.html#chr</a>), keeping only the alphanumeric characters.</p>

<p>On Linux, <em>$man hexdump</em> on a terminal.</p>

python