Use the following function:
<?php function bin2hex($str) { $hex = ""; $i = 0; do { $hex .= dechex(ord($str{$i})); $i++; } while ($i < strlen($str)); return $hex; } // Look what happens when ord($str{$i}) is 0...15 // you get a single digit hexadecimal value 0...F // bin2hex($str) could return something like 4a3, // decimals(74, 3), whatever the binary value is of those. function hex2bin($str) { $bin = ""; $i = 0; do { $bin .= chr(hexdec($str{$i}.$str{($i + 1)})); $i += 2; } while ($i < strlen($str)); return $bin; } // hex2bin("4a3") just broke. Now what? // Using sprintf() to get it right. function bin2hex($str) { $hex = ""; $i = 0; do { $hex .= sprintf("%02x", ord($str{$i})); $i++; } while ($i < strlen($str)); return $hex; } // now using whatever the binary value of decimals(74, 3) // and this bin2hex() you get a hexadecimal value you can // then run the hex2bin function on. 4a03 instead of 4a3. ?>
source share