Convert from ascii to hex in php
ascii, hex, php
Solution
Use that:
function ascii2hex($ascii) {
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
return $hex;
}
The result:
Problem
I'm trying to convert from ASCII to HEX in PHP but get a different result to some of the online tools that are available. I know the result I'm looking for so the online tool's result appear to be correct and my code incorrect but I can't work out why. ``` String: 2Ffbj?DoyXOU Correct output: 32 46 66 62 6a 3f 44 6f 79 58 4f 55 (from linked site above) My output: 32 46 66 62 6a 3f 44 6f 79 58 4f 75 ``` My script: ``` echo bin2hex(utf8_decode("2Ffbj?DoyXOU")); ``` Where is the fault?