<?php
/*
 * Convert a hex string to ASCII with hexstr()
 * Convert a ASCII string to a hex string with strhex()
 * 
 *
 * Paul Gregg <pgregg@pgregg.com>
 * 3 October 2003
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/code/hexstr.phps
 *
 */

Function hexstr($hexstr) {
  
$hexstr str_replace(' '''$hexstr);
  
$retstr pack('H*'$hexstr);
  return 
$retstr;
}

Function 
strhex($string) {
  
$hexstr unpack('H*'$string);
#echo '<pre>', var_dump($hexstr), '</pre>';
  
return array_pop($hexstr);
}

$teststr "64 65 74 61 69 6c 73";
$str hexstr($teststr);
$hex strhex($str);
print 
"<pre>";
print 
"Original Hex: $teststr\n";
print 
"Result from hexstr: "var_dump$str ); print "\n";
print 
"Result from strhex: "var_dump$hex ); print "\n";

if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
  
$q $_POST['q'];
  if (
$_POST['submit'] == 'hexstr')
    
$r hexstr($q);
  else
    
$r strhex($q);
  print 
"Input value ($q) -> ".$_POST['submit']." = $r\n<br>";

}

print 
"<form method='POST' action='${_SERVER['PHP_SELF']}'>\n";
print 
"<textarea name='q' rows=10 cols=80>$r</textarea>";
print 
"<input type='submit' name='submit' value='hexstr'>";
print 
"<input type='submit' name='submit' value='strhex'>";
print 
"</form>\n";

?>