Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, struct
- # do we make a 32 or 16 bits bytes swap?
- targetswap = 16
- bytestoconsider = int(targetswap / 8)
- if targetswap == 32:
- pack = '>I'
- unpack = '<I'
- else:
- pack = '>H'
- unpack = '<H'
- if len(sys.argv) > 1:
- baseName = sys.argv[1]
- try:
- with open(baseName, mode='rb') as InFile:
- OutFile = open("%s.bs" % (baseName), "wb")
- file_data = InFile.read()
- sz = len(file_data)
- if sz % bytestoconsider:
- print("Be carefull, %d bytes will not be present in destination" % (sz % bytestoconsider))
- for i in range(0, (sz // bytestoconsider) * bytestoconsider, bytestoconsider):
- val = struct.unpack(unpack,file_data[i:i+bytestoconsider])
- swapped = struct.pack(pack, val[0])
- OutFile.write(swapped)
- OutFile.close()
- except:
- print("Cannot open file '%s'!" % (baseName))
- else:
- print("Please give the source file name as parameter")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement