How can I get a hex dump of a string in PHP?

后端 未结 6 1248
陌清茗
陌清茗 2020-11-22 07:50

I\'m investigating encodings in PHP5. Is there some way to get a raw hex dump of a string? i.e. a hex representation of each of the bytes (not characters) in a string?

6条回答
  •  迷失自我
    2020-11-22 08:09

    echo bin2hex($string);
    

    or:

    for ($i = 0; $i < strlen($string); $i++) {
        echo str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
    }
    

    $string is the variable which contains input.

提交回复
热议问题