在php中从ascii转换为hex

Sam*_*Maj 2 php hex ascii

我正在尝试在PHP中从ASCII转换为HEX,但是对于一些可用的在线工具获得了不同的结果.我知道我正在寻找的结果,所以在线工具的结果似乎是正确的,我的代码不正确,但我无法解决原因.

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
Run Code Online (Sandbox Code Playgroud)

我的剧本:

echo bin2hex(utf8_decode("2Ffbj?DoyXOU"));
Run Code Online (Sandbox Code Playgroud)

故障在哪里?

Pat*_*iel 8

使用:

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;
}
Run Code Online (Sandbox Code Playgroud)

结果:

ascii到hex

  • 你能解释为什么 OPs 方式没有返回正确的结果吗? (2认同)