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

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False