如何将看起来像十六进制数的字符串转换为php中的实际十六进制数?

时间:2021-04-27 15:45:34

I have a string that looks like this "7a" and I want to convert it to the hex number 7A. I have tried using pack and unpack but that is giving me the hex representation for each individual character.

我有一个看起来像这个“7a”的字符串,我想将其转换为十六进制数字7A。我已经尝试过使用pack和unpack,但是这给了我每个角色的十六进制表示。

3 个解决方案

#1


22  

Probably the simplest way to store that as an integer is hexdec()

将整数存储为整数的最简单方法可能是hexdec()

$num = hexdec( '7A' );

#2


8  

Well a number is a number, it does not depend on the representation. You can get the actual value using intval():

数字是一个数字,它不依赖于表示。您可以使用intval()获取实际值:

$number = intval('7a', 16); 

To convert the number back to a hexadecimal string you can use dechex().

要将数字转换回十六进制字符串,可以使用dechex()。

#3


0  

This can by try -

这可以试试 -

function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
    $hex .= dechex(ord($string[$i]));
}
return $hex;
}

#1


22  

Probably the simplest way to store that as an integer is hexdec()

将整数存储为整数的最简单方法可能是hexdec()

$num = hexdec( '7A' );

#2


8  

Well a number is a number, it does not depend on the representation. You can get the actual value using intval():

数字是一个数字,它不依赖于表示。您可以使用intval()获取实际值:

$number = intval('7a', 16); 

To convert the number back to a hexadecimal string you can use dechex().

要将数字转换回十六进制字符串,可以使用dechex()。

#3


0  

This can by try -

这可以试试 -

function strToHex($string)
{
$hex='';
for ($i=0; $i < strlen($string); $i++)
{
    $hex .= dechex(ord($string[$i]));
}
return $hex;
}