Advertisement
waliedassar

HexToFile.Py

Apr 27th, 2015
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. # Takes a Hex-Encoded string and outputs it in Hex
  2. # Blog Post:
  3.  
  4. import sys,os,time
  5.  
  6. def IsNonHexChar(CharX):
  7.     if CharX != "0" and CharX != "1" and CharX != "2" and CharX != "3" and CharX != "4" and CharX != "5" and CharX != "6" and CharX != "7" and CharX != "8" and CharX != "9" and CharX != "A" and CharX != "a" and CharX != "B" and CharX != "b" and CharX != "C" and CharX != "c" and CharX != "D" and CharX != "d" and CharX != "E" and CharX != "e" and CharX != "F" and CharX != "f":
  8.         return True
  9.     return False
  10.  
  11.  
  12. def Hexify(StuffX):
  13.     if len(StuffX)==0:
  14.         print "File is empty\r\n"
  15.         return
  16.     else:
  17.         Second = False
  18.         SkipNext = False
  19.         FinalStr = ""
  20.         NewStr = ""
  21.         for X in StuffX:
  22.             if SkipNext == True:
  23.                 SkipNext = False
  24.                 continue
  25.             if IsNonHexChar(X)==True:
  26.                 SkipNext = True
  27.                 continue
  28.             if Second == False:
  29.                 NewStr+=X
  30.                 Second = True
  31.             else:
  32.                 NewStr+=X
  33.                 FinalStr += "\\x"
  34.                 FinalStr += NewStr
  35.                 NewStr = ""
  36.                 Second = False
  37.        
  38.         XXX = "\"" + FinalStr + "\""
  39.         outputX =  eval(XXX)
  40.         return outputX
  41.  
  42. def RevvAndThenHexify(InputFileX):
  43.     if os.path.exists(InputFileX)==False:
  44.         print "File does not exist\r\n"
  45.         return
  46.     fIn = open(InputFileX,"r")
  47.     contentX = fIn.read()
  48.     fIn.close()
  49.     if len(contentX)==0:
  50.         print "File is empty\r\n"
  51.         return
  52.     else:
  53.         print "Processing input of length " + str(len(contentX))
  54.  
  55.     fOut = open("result.txt","wb")
  56.     fIn_ = open(InputFileX,"r")
  57.     for LinX in fIn_:
  58.         PureStringX = LinX.rstrip("\r\n")
  59.         PureStringX = PureStringX.rstrip("\r")
  60.         PureStringX = PureStringX.rstrip("\n")
  61.         print len(''.join(reversed(PureStringX)))
  62.         Hexified = Hexify(PureStringX)
  63.         print "Line of Length: " + str(len(Hexified))
  64.         fOut.write(Hexified)
  65.     fIn_.close()
  66.     fOut.close()
  67.     return
  68.  
  69. def main():
  70.     if len(sys.argv)!=2:
  71.         print "Usage: Hexify.py input.txt\r\n"
  72.         sys.exit(-1)
  73.     else:
  74.         RevvAndThenHexify(sys.argv[1])
  75.         sys.exit(0)
  76.        
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement