Advertisement
bf17

Byte frequency of text or binary file

Apr 12th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #python 3.5
  2. # cf1 - 1st order character distribution
  3. # text or binary files
  4.  
  5. #set up array
  6. from array import array
  7. a = array("H")
  8.  
  9. # ASCII
  10. def printable(n):
  11.     if((n>31 and n<127)):
  12.         return(True)
  13.     else:
  14.         return(False)
  15.  
  16. input_file = input("file name: ")
  17.  
  18. f = open(input_file, 'rb')
  19.  
  20. # creat and initialize array
  21. a = [0 for i in range(256)]
  22.  
  23. while True:
  24.     nextch = f.read(1)
  25.     if(not nextch): break
  26.    
  27.     a[ord(nextch)] += 1
  28.    
  29. for i in range(256):
  30.     if(a[i] > 0):
  31.         if(printable(i)):
  32.             print("'" + chr(i) + "'", "\t", a[i])
  33.         else:
  34.             print(i, "\t", a[i])
  35.  
  36. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement