php string转hex,PHP将字符串转换为十六进制,并将十六进制转换为字符串

PHP将字符串转换为十六进制,并将十六进制转换为字符串

在PHP中在这2种类型之间进行转换时遇到了问题。 这是我在谷歌搜索的代码

function strToHex($string){

$hex='';

for ($i=0; $i < strlen($string); $i++){

$hex .= dechex(ord($string[$i]));

}

return $hex;

}

function hexToStr($hex){

$string='';

for ($i=0; $i < strlen($hex)-1; $i+=2){

$string .= chr(hexdec($hex[$i].$hex[$i+1]));

}

return $string;

}

我检查了一下,并在使用XOR加密时发现了这一点。

我有一个字符串"this is the test",在与键进行XOR之后,我在字符串↕↑↔§P↔§P ♫§T↕§↕中得到了结果。之后,我尝试通过函数strToHex()将其转换为十六进制,并得到了这些12181d15501d15500e15541215712。然后,我使用了函数hexToStr( ),我有↕↑↔§P↔§P♫§T↕§q。那么,我应该怎么做才能解决此问题? 转换这2个样式值时为什么会出错?

JoeNguyen asked 2020-08-06T02:12:48Z

8个解决方案

48 votes

对于ord($ char)<16的任何char,您将获得仅1长的十六进制返回值。 您忘记添加0填充。

这应该解决它:

function strToHex($string){

$hex = '';

for ($i=0; $i

$ord = ord($string[$i]);

$hexCode = dechex($ord);

$hex .= substr('0'.$hexCode, -2);

}

return strToUpper($hex);

}

function hexToStr($hex){

$string='';

for ($i=0; $i < strlen($hex)-1; $i+=2){

$string .= chr(hexdec($hex[$i].$hex[$i+1]));

}

return $string;

}

// Tests

header('Content-Type: text/plain');

function test($expected, $actual, $success) {

if($expected !== $actual) {

echo "Expected: '$expected'\n";

echo "Actual: '$actual'\n";

echo "\n";

$success = false;

}

return $success;

}

$success = true;

$success = test('00', strToHex(hexToStr('00')), $success);

$success = test('FF', strToHex(hexToStr('FF')), $success);

$success = test('000102FF', strToHex(hexToStr('000102FF')), $success);

$success = test('↕↑↔§P↔§P ♫§T↕§↕', hexToStr(strToHex('↕↑↔§P↔§P ♫§T↕§↕')), $success);

echo $success ? "Success" : "\nFailed";

boomla answered 2020-08-06T02:12:58Z

36 votes

对于最终到这里并且只是在寻找(二进制)字符串的十六进制表示的人。

bin2hex("that's all you need");

# 74686174277320616c6c20796f75206e656564

hex2bin('74686174277320616c6c20796f75206e656564');

# that's all you need

Philippe Gerber answered 2020-08-06T02:13:19Z

28 votes

PHP的:

十六进制字符串:

implode(unpack("H*", $string));

十六进制到字符串:

pack("H*", $hex);

زياد answered 2020-08-06T02:13:47Z

13 votes

这是我使用的:

function strhex($string) {

$hexstr = unpack('H*', $string);

return array_shift($hexstr);

}

Bill Shirley answered 2020-08-06T02:14:07Z

1 votes

function hexToStr($hex){

// Remove spaces if the hex string has spaces

$hex = str_replace(' ', '', $hex);

return hex2bin($hex);

}

// Test it

$hex = "53 44 43 30 30 32 30 30 30 31 37 33";

echo hexToStr($hex); // SDC002000173

/**

* Test Hex To string with PHP UNIT

* @param string $value

* @return

*/

public function testHexToString()

{

$string = 'SDC002000173';

$hex = "53 44 43 30 30 32 30 30 30 31 37 33";

$result = hexToStr($hex);

$this->assertEquals($result,$string);

}

Kamaro Lambert answered 2020-08-06T02:14:22Z

0 votes

我只有一半的答案,但是我希望它会有用,因为它增加了对unicode(utf-8)的支持

//decimal to unicode character

function unichr($dec) {

if ($dec < 128) {

$utf = chr($dec);

} else if ($dec < 2048) {

$utf = chr(192 + (($dec - ($dec % 64)) / 64));

$utf .= chr(128 + ($dec % 64));

} else {

$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));

$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));

$utf .= chr(128 + ($dec % 64));

}

return $utf;

}

var_dump(unichr(hexdec('e641')));

来源:[http://www.php.net/manual/en/function.chr.php#Hcom55978]

Timo Huovinen answered 2020-08-06T02:14:52Z

0 votes

使用@ bill-shirley答案并添加一些内容

function str_to_hex($string) {

$hexstr = unpack('H*', $string);

return array_shift($hexstr);

}

function hex_to_str($string) {

return hex2bin("$string");

}

用法:

$str = "Go placidly amidst the noise";

$hexstr = str_to_hex($str);// 476f20706c616369646c7920616d6964737420746865206e6f697365

$strstr = hex_to_str($str);// Go placidly amidst the noise

PeterT answered 2020-08-06T02:15:18Z

0 votes

您可以尝试以下代码将图像转换为十六进制字符串

$image = 'sample.bmp';

$file = fopen($image, 'r') or die("Could not open $image");

while ($file && !feof($file)){

$chunk = fread($file, 1000000); # You can affect performance altering

this number. YMMV.

# This loop will be dog-slow, almost for sure...

# You could snag two or three bytes and shift/add them,

# but at 4 bytes, you violate the 7fffffff limit of dechex...

# You could maybe write a better dechex that would accept multiple bytes

# and use substr... Maybe.

for ($byte = 0; $byte < strlen($chunk); $byte++)){

echo dechex(ord($chunk[$byte]));

}

}

?>

Bhargav Venkatesh answered 2020-08-06T02:15:38Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值