开发者

PHP: create file from an HEX string

开发者 https://www.devze.com 2023-02-24 09:36 出处:网络
I have the content of a file represented as an Hex string and I want to re-create that file from that hex string. How can I do it?

I have the content of a file represented as an Hex string and I want to re-create that file from that hex string. How can I do it?

Additional information

开发者_如何学JAVA The hex string is taken from firefox memory cache entries such as the one below:

about:cache-entry?client=HTTP&sb=1&key=http://www.google.co.uk/images/nav_logo40.png

I am processing the <pre> element from these entries to extract and concatenate the HEX values. So from the below data output line obtained from a cache entry:

00000000:  89  50  4e  47  0d  0a  1a  0a  00  00  00  0d  49  48  44  52  .PNG........IHDR

I am producing this

89504e470d0a1a0a0000000d49484452

I am repeating the same process for every line so that I end up with 1 big string containing all hex values concatenated.


You can use pack:

file_put_contents($filename, pack('H*', $hex));

That will turn a string of octets in hexadecimal notation into binary:

var_dump(pack('H*', '313233'));  // string(3) "123"
0

精彩评论

暂无评论...
验证码 换一张
取 消