Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the PHP equivalent of MySQL's UNHEX()?

Tags:

php

mysql

What is the PHP equivalent of MySQL's UNHEX()?

For instance, the following query and PHP function should provide the same value.

SELECT UNHEX(c1) AS unhexed_c1 FROM table;

$unhexed_c1=PHPs_UNHEX_Equivalent($c1);
like image 885
user1032531 Avatar asked Sep 28 '13 20:09

user1032531


3 Answers

There's a built-in function called hex2bin if you're running PHP >= 5.4.

like image 194
Joel Hinz Avatar answered Oct 29 '22 12:10

Joel Hinz


It can be done with pack:

$unhexed = pack('H*', $hexstring);
like image 10
dev-null-dweller Avatar answered Oct 29 '22 11:10

dev-null-dweller


See How to convert hex to string or text in php:

  function unhex($hex) {
    for($i=0;$i<strlen($hex);$i+=2)
       $str .= chr(hexdec(substr($hex,$i,2)));

    return $str;
  }
like image 5
dajavax Avatar answered Oct 29 '22 12:10

dajavax