【问题标题】:PHP convert string to "\xnn" hex [duplicate]PHP将字符串转换为“\ xnn”十六进制[重复]
【发布时间】:2019-02-14 23:06:08
【问题描述】:

如何将字符串 'Hello world' 转换为 '\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21' 使用 php?

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    应该有效

    function strhex($string) {
      $hexstr = unpack('H*', $string);
      return array_shift($hexstr);
    }
    

    更新

    下面的代码是你需要的

    function strtohex($string)
    {
      $string = str_split($string);
      foreach($string as &$char)
        $char = "\x".dechex(ord($char));
      return implode('',$string);
    }
    
    print strtohex("[0-9A-Za-z\+/=]*");
    

    【讨论】:

    • 几乎但不完全。它给出了 48656c6c6f20776f726c64。 3v4l.org/68Cho
    • echo(strhex('Hello World!')); 返回'48656c6c6f20576f726c6421'
    • bin2hex 已经在本地完成了这项工作
    • @Devon 我怎样才能得到 \x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21??告诉我
    • @FunnyCheese 我更新了我的答案
    猜你喜欢
    • 2018-07-03
    • 2020-11-07
    • 2011-05-15
    • 2019-01-17
    • 2023-03-08
    • 2012-07-21
    • 2017-05-29
    • 2013-01-16
    相关资源
    最近更新 更多