3

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
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.

3
  • Expand your question. What has been "hexed"? Are you talking about encoding of binary values of the file or something else? Commented Apr 14, 2011 at 11:54
  • please see Additional information Commented Apr 14, 2011 at 15:06
  • You can use this little script for extracting cache entries of firefox and chrome. Commented Oct 17, 2015 at 17:25

1 Answer 1

6

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"
Sign up to request clarification or add additional context in comments.

2 Comments

I tested your method but it doesn't work. Could you please re-assess my question taking into account what I added into Additional information?
Indeed it does. My mistake. My script wasn't getting the hex output properly. All fixed now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.