0

I want to convert hex string to ASCII, and I tried multiple ways to convert it but it throws the same error every time.

The code:

hexFile = open('message', 'rb')
fileData = hexFile.read()
dataString = str(fileData, 'UTF-8')
bytes_object = bytes.fromhex(dataString)
ascii_string = bytes_object.decode("ASCII")
print(ascii_string)

The error:

line 130, in dataString = str(fileData, 'UTF-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf3 in position 2: invalid continuation byte

bad_coder
  • 8,684
  • 19
  • 37
  • 59
Rasha
  • 1
  • 1

1 Answers1

0

you can use binascii moduel for this

import binascii
with open('message','rb') as f:
    print(binascii.b2a_uu(f.read()).decode()) #decode will return string from bytes

but remember it can only do length of data at most 45. for more you can use binascii.b2a_base64. see docs

gaurav
  • 1,239
  • 1
  • 13
  • 24
  • 1
    The problem is that the code assumes the data a bunch of hexadecimal digits encoded in UTF-8, and it isn't. Print out the first couple of bytes of `filedata` and include it in your question so that you (and we) can see what is really in the file. – BoarGules Jun 13 '21 at 07:52