Skip to content

Instantly share code, notes, and snippets.

@bhanpuramufaddal
Created July 11, 2023 08:05
Show Gist options
  • Select an option

  • Save bhanpuramufaddal/ae768811542dd06bdc45feb9f34390cc to your computer and use it in GitHub Desktop.

Select an option

Save bhanpuramufaddal/ae768811542dd06bdc45feb9f34390cc to your computer and use it in GitHub Desktop.
Convert hex characters in a string back
def convert_hex_char(string):
decoded_string = bytes(string, 'utf-8').decode('unicode-escape')
return decoded_string
import re
def convert_hex_string(string):
hex_pattern = r'\\x([0-9a-fA-F]{2})'
hex_matches = re.findall(hex_pattern, string)
for hex_match in hex_matches:
string = string.replace(f'\\x{hex_match}', chr(int(hex_match, 16)))
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment