4

How do I convert a hexadecimal text to a regular readable (varchar) string in MySQL?

Ive seen some functions to convert hex to numbers but nothing to string.

riahc3
  • 857
  • 4
  • 10
  • 23

2 Answers2

6

Answer is:

select CAST(unhex('hexstringhere') AS CHAR(100))

Figured it out via trial and error.

riahc3
  • 857
  • 4
  • 10
  • 23
2

Use the function UNHEX

For a string argument str, UNHEX(str) interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number. The return value is a binary string.

mysql> SELECT UNHEX('4D7953514C');
        -> 'MySQL'
mysql> SELECT 0x4D7953514C;
        -> 'MySQL'
mysql> SELECT UNHEX(HEX('string'));
        -> 'string'
mysql> SELECT HEX(UNHEX('1267'));
        -> '1267'

Font: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_unhex

S. Moreno
  • 526
  • 2
  • 7
  • 29