[Tutoriel] Formatage d'une chaîne de caractère avec Python3



Ce script peut convertir:

  • Une chaîne de caractère en sa représentation hexadécimale
  • Hexadécimal en chaîne de caractère
  • Une chaîne de caractère en sa représentation binaire
  • Binaire en chaîne de caractère
  • Binaire en hexadécimal
  • Hexadécimal en binaire
  • Inverser une chaîne de caractère
  • Chaîne de caractère en sa représentation en base64
  • Base64 en chaîne de caractère


#!/usr/bin/python3
#
# Some useful methods
# convert :
# - String <-> Binary
# - String <-> Hex
# - Reverse string (the most pythonic and rapid way using slices)
# - String <-> base64
# - Binary <-> Hex
class ConvertTool:
def __init__(self):
pass
def stringToHex(self, input_str = ""):
try:
return ''.join('{0}'.format(hex(ord(char)).replace('0x', "")) for char in input_str)
except Exception as e:
print("Error occurred during convertion of {0} into a Hex\nError: {1} ".format(input_str, e))
return None
def hexToString(self, input_str = ""):
try:
return ''.join('{0}'.format(chr(int(x, 16))) for x in [input_str[i:i+2] for i in range(0, len(input_str), 2)])
except Exception as e:
print("Error occurend during the convertion of {0} into a string\nError: {1}".format(input_str, e))
return None
def stringToBinary(self, input_str = ""):
try:
return ''.join('{0:08b}'.format(ord(char), 'b') for char in input_str )
except Exception as e:
print("Error occurred druing the convertion of {0} to Binary\nError: {1}".format(input_str, e))
return None
def binaryToString(self, input_str = ""):
# Convert binary into hex then convert hex into string
try:
return self.hexToString(self.binaryToHex(input_str))
except Exception as e:
print("Error occurred during the convertion of {0} into a String\nError: {1}".format(input_str, e))
return None
def binaryToHex(self, input_str = ""):
try:
return "{0:0>4x}".format(int(input_str, 2))
except Exception as e:
print("Error occurred during the convertion of {0} into a Hex\nError: {1}".format(input_str, e))
return None
def hexToBinary(self, input_str = ""):
try:
return '{0:0>4b}'.format(int(input_str, 16))
except Exception as e:
print("Error occurred during the convertion of {0} into a Binary\nError: {1}".format(input_str, e))
return None
def reverseString(self, input_str = ""):
try:
return input_str[::-1]
except Exception as e:
print("Error occurred during reversing {0}\nError: {1}".format(input_str, e))
return None
def stringToBase64(self, input_str = ""):
try:
from base64 import b64encode
return b64encode(input_str.encode()).decode('utf8')
except Exception as e:
print("Error occurred during encoding {0} into base64\nError: {1}".format(input_str, e))
return None
def base64ToString(self, input_str = ""):
try:
from base64 import b64decode
return b64decode(input_str.encode()).decode('utf8')
except Exception as e:
print("Error occurred during decoding {0} into String\nError: {1}".format(input_str, e))
return None
# Test
if __name__ == '__main__':
app = ConvertTool()
print("string -> hex: ", app.stringToHex("ÿÿa"))
print("hex -> string : ", app.hexToString("6161"))
print("string -> binary: ", app.stringToBinary("ÿÿa"))
print("binary -> hex: ", app.binaryToHex("1111111111111111"))
print("binary -> string: ", app.binaryToString("1111111111111111"))
print("hex -> binary: ", app.hexToBinary("ffff61"))
print("reversed string: ", app.reverseString("hello nexus "))
print("string -> base64: ", app.stringToBase64("chiheb nexus"))
print("base64 -> string: ", app.base64ToString("Y2hpaGViIG5leHVz"))
view raw convertTool.py hosted with ❤ by GitHub

Enregistrer un commentaire

Remarque : Seul un membre de ce blog est autorisé à enregistrer un commentaire.