且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP字符串到十六进制

更新时间:2023-02-03 08:29:55

  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\ + / =] *);

上述代码会给你

  \x5b\x30\x2d\x39\x41\x2d\x5a\x61\x2d\x7a\x5c\x2b\x2f\ x3d \x5d\x2a 

我知道它看起来不像输出expect,但是似乎不是字符串到十六进制。


I have a string like that:

[0-9A-Za-z\+/=]*

How can I converted in the following form:

"\133\x30\55\x39\101\x2d\132\x61\55\x7a\134\x2b\57\x3d\135\x2a"

Is there any function for that ?

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\+/=]*");

The above code will give you

\x5b\x30\x2d\x39\x41\x2d\x5a\x61\x2d\x7a\x5c\x2b\x2f\x3d\x5d\x2a

I'm aware that it doesn't look like the output you expect, but that doesn't seem to be string to hex at all.