Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys, struct
  4.  
  5. # do we make a 32 or 16 bits bytes swap?
  6. targetswap = 16
  7.  
  8. bytestoconsider = int(targetswap / 8)
  9. if targetswap == 32:
  10.     pack = '>I'
  11.     unpack = '<I'
  12. else:
  13.     pack = '>H'
  14.     unpack = '<H'
  15.  
  16. if len(sys.argv) > 1:
  17.     baseName = sys.argv[1]
  18.     try:
  19.         with open(baseName, mode='rb') as InFile:
  20.             OutFile = open("%s.bs" % (baseName), "wb")
  21.             file_data = InFile.read()
  22.             sz = len(file_data)
  23.             if sz % bytestoconsider:
  24.                 print("Be carefull, %d bytes will not be present in destination" % (sz % bytestoconsider))
  25.             for i in range(0, (sz // bytestoconsider) * bytestoconsider, bytestoconsider):
  26.                 val = struct.unpack(unpack,file_data[i:i+bytestoconsider])
  27.                 swapped = struct.pack(pack, val[0])
  28.                 OutFile.write(swapped)      
  29.         OutFile.close()
  30.     except:
  31.         print("Cannot open file '%s'!" % (baseName))
  32. else:
  33.     print("Please give the source file name as parameter")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement