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

后端 未结 6 1130
陌清茗
陌清茗 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:08

    While debugging a binary protocol I needed a hexdump() too. I decided to publish my solution as a PEAR package as it's definitely useful. You can also browse the code at github.

    PEAR : http://www.metashock.de/pear

    GitHub : http://www.github.com/metashock/Hexdump

    In addition to mindplays solution it supports propper rendering of the last line and additional params. Also the package contains a php executable named phphd for hexdumps on cmdline. This might be helpful on Windows Systems :)

    @mindplay.dk : Thanks for the strtr() idea. It appeared slight faster than my former attempt. Integrated that into my version. (Using a decreased translation buffer)..

    Have Fun!

提交回复
热议问题