Casa > H > How To Convert Hex Into A String Using Python

How to convert hex into a string using Python

There are a few ways depending on the Python version you have on your computer.

Python 2

  1. >>> "(HexValue)".decode("hex") 
  2. 'string' 
  3.  
  4. >>> "7368616b6564".decode("hex") 
  5. 'shaked' 

Python 2 + 3

  1. >>> bytearray.fromhex("HexValue").decode() 
  2. 'string' 
  3.  
  4. >>> bytearray.fromhex("7368616b6564").decode() 
  5. 'shaked' 

Python 3

  1. >>> bytes.fromhex('HexValue').decode('utf-8') 
  2. 'string' 
  3.  
  4. >>> bytes.fromhex('7368616b6564').decode('utf-8') 
  5. 'shaked' 

You can also use Print to convert hex values to text (Add () in print Python 3)

  1. >>> print "HexValues" 
  2. 'string' 
  3.  
  4. >>> print "\x73 \x68 \x61 \x6b \x65 \x64" 
  5. 'shaked' 

More Information

Convert from ASCII string encoded in Hex to plain ASCII?

Python: hex string to char

De Payne Schulthess

O que são algumas bolsas de estudo que vale a pena ver? :: A Fulton Books é uma editora legítima?