douji1058 2011-04-14 11:52
浏览 70
已采纳

PHP:从HEX字符串创建文件

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.

  • 写回答

1条回答 默认 最新

  • doushenjia8514 2011-04-14 11:57
    关注

    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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)