Convert hex string to characters in python3

jakebacker44

I need to convert a string such as 5555555547aa into a string of characters (that one would be �GUUUU). I have a function working in python2 which is posted below, but I can't get anything to work right in python3. How would I do this?

    def hex_conv(hex_str):
      arr = [hex_str[i:i+2] for i in range(0, len(hex_str), 2)]
      rev = arr[::-1]
      out_str = ""
      for i in rev:
        print(i)
        out_str += ("\\x" + i).decode("string_escape")
      return out_str

More Clearly of what I need outputted (in Python2):

print('\xaa\x47\x55\x55\x55\x55')

I have tried

print(bytes.fromhex("5555555547aa")[::-1].decode("utf-8", errors="replace"))

which doesn't quite work. It appears to produce the same output but the actual bytes are different.

Note: The output of this is going to be piped into another program (for binary exploitation) so the bytes need to be printed exact.

SuperStormer

You just need to combine 2 steps: convert from hex to a bytestring with bytes.fromhex, and to print a bytestring to stdout using sys.stdout.buffer.write. Putting it all together:

import sys
sys.stdout.buffer.write(bytes.fromhex("5555555547aa")[::-1])

Sourced from hexadecimal string to byte array in python and How to write a raw hex byte to stdout in Python 3?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert string to HEX characters

How to convert python byte string containing a mix of hex characters?

Python: Convert hex to string

convert string to hex in python

Convert string with special characters to hex - C#

Convert string with escaped characters and ASCII values into HEX

Python convert a string containing hex to actual hex

Python - convert from hex integer to hex string

Convert hex string to int in Python

Convert hex to decimal/string in python

XSLT: Convert specific characters of a string individually to a string *including* their values in hex

Convert hex Ascii representation in a bytes object to Ascii string using Python3

C++ how to convert string characters to exact hex bytes

Remove all hex characters from string in Python

Parse hex and ASCII characters into string in Python

Represent string characters as hex values in python

String of hex characters to actual hex characters

C string and hex characters

Convert a hex string to a hex int

Convert hex string to usably hex

Python3 bytes to hex string

Arduino: Convert a String hex "#FFFFFF" into 3 int

How to convert a string with unicode characters in a string with utf-8 hex characters?

Python convert Hex string to Signed Integer

How to convert a full ascii string to hex in python?

Python Pandas Subset Hex String, Convert to Decimal

How to convert hex string to color image in python?

convert hex to ascii characters xslt

How can I convert unicode string contain characters that out-of-range utf-8 or 16 to binary or hex in python?