UY-Scuti

Untitled

Jun 18th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Converts to hex, ascii, decimal, octal, binary, base64, or little-endian.
  4.  
  5. import sys
  6. from binascii import unhexlify, b2a_base64
  7.  
  8.  
  9. def ascToHex(string):
  10. in_hex = string.encode('hex')
  11. return in_hex
  12.  
  13.  
  14. def toLittleEndian(string):
  15. little_endian = '0x' + "".join(reversed([string[i:i+2]
  16. for i in range(0, len(string), 2)]))
  17. return little_endian
  18.  
  19.  
  20. def toDecimal(string):
  21. in_dec = int(string, 16)
  22. return in_dec
  23.  
  24.  
  25. def toAscii(string):
  26. in_ascii = string.decode('hex')
  27. return in_ascii
  28.  
  29.  
  30. def toOctal(string):
  31. in_oct = ""
  32. c = 0
  33. for char in string:
  34. c = ord(char)
  35. octa = oct(c)
  36. in_oct += ' ' + str(octa)
  37. return in_oct
  38.  
  39.  
  40. def hexToBin(string):
  41. in_hex = int(string, 16)
  42. in_bin = bin(in_hex)[2:]
  43. return in_bin
  44.  
  45.  
  46. def binToHex(string):
  47. in_hex = hex(int(string, 2))
  48. return in_hex
  49.  
  50.  
  51. def decToHex(number):
  52. in_hex = hex(int(number))
  53. return in_hex
  54.  
  55.  
  56. def hexToB64(string):
  57. raw = unhexlify(string)
  58. in_b64 = b2a_base64(raw)
  59. return in_b64
  60.  
  61.  
  62. def main():
  63. if len(sys.argv[1:]) != 2:
  64. print '''
  65. format_convert: Convert to hex, ascii, decimal, octal, binary, base64 or little-endian.
  66. Usage: ./format_convert.py <string> <option>
  67. Example: ./format_convert.py 41 -2ascii
  68. Options:
  69. -asc2hex : Ascii to hex
  70. -2ascii : Hex to ascii
  71. -2dec : To decimal
  72. -2oct : To octal
  73. -2le : Big endian to little endian
  74. -hex2bin : Hex to binary
  75. -bin2hex : Binary to hex
  76. -dec2hex : Decimal to hex
  77. -hex2b64 : Hex to base64
  78. '''
  79. sys.exit(0)
  80.  
  81. # Original input
  82. to_convert = sys.argv[1]
  83. mode = sys.argv[2]
  84.  
  85. # Conversion
  86. if mode == '-asc2hex':
  87. in_hex = ascToHex(to_convert)
  88. little_endian = toLittleEndian(in_hex)
  89. print 'Original:', to_convert, '\nHex:', '0x' + in_hex
  90. print 'Little-endian:', little_endian
  91.  
  92. elif mode == '-2ascii':
  93. in_ascii = toAscii(to_convert)
  94. print 'Original:', to_convert, '\nAscii:', in_ascii
  95.  
  96. elif mode == '-2dec':
  97. in_dec = toDecimal(to_convert)
  98. print 'Original:', to_convert, '\nDecimal:', in_dec
  99.  
  100. elif mode == '-2oct':
  101. in_oct = toOctal(to_convert)
  102. print 'Original:', to_convert, '\nOctal:', in_oct, '\n\n[!] Note: Remove any extra leading zeroes.'
  103.  
  104. elif mode == '-2le':
  105. inpt = toAscii(to_convert)
  106. in_hex = ascToHex(inpt)
  107. in_LE = toLittleEndian(in_hex)
  108. print 'Original:', '0x' + to_convert, '\nLittle-endian:', in_LE
  109.  
  110. elif mode == '-hex2bin':
  111. in_bin = hexToBin(to_convert)
  112. print 'Originial:', to_convert, '\nBinary:', in_bin
  113.  
  114. elif mode == '-bin2hex':
  115. in_hex = binToHex(to_convert)
  116. print in_hex
  117.  
  118. elif mode == '-dec2hex':
  119. in_hex = decToHex(to_convert)
  120. print in_hex
  121.  
  122. elif mode == '-hex2b64':
  123. in_b64 = hexToB64(to_convert)
  124. print in_b64
  125.  
  126. else:
  127. print 'Improper format. Review and re-submit.\n'
  128. sys.exit(0)
  129.  
  130.  
  131. main()
Advertisement
Add Comment
Please, Sign In to add comment