[英]64 Binary String to 16 character hex in PHP
I have a 64 character long string, like the following: 0100101000000011111010000101010000111101000011010100010000100110
That I need to end up in what I believe is a hexidecimal string that is 18 characters long: 084a0002d43d0d4426
. 我有一个64个字符的长字符串,如下所示:
0100101000000011111010000101010000111101000011010100010000100110
我需要结束我认为是18个字符长的十六进制字符串: 084a0002d43d0d4426
。
The code that currently does this in C# looks like this: 当前在C#中执行此操作的代码如下所示:
private string BinaryStringToHexString(string binary)
{
StringBuilder result = new StringBuilder(binary.Length / 8 + 1);
for (int i = 0; i < binary.Length; i += 8)
{
string eightBits = binary.Substring(i, 8);
result.AppendFormat("{0:X2}", Convert.ToByte(eightBits, 2));
}
return result.ToString();
}
I'm trying to figure out how this is achieved in PHP, but when using the dechex
or binhex
functions in php the format does not end up similar. 我试图弄清楚这是如何在PHP中实现的,但是在php中使用
dechex
或binhex
函数时,格式却不会相似。 Here is my current attempt in PHP: 这是我目前在PHP中的尝试:
$fullStringToEncode = '0100101000000011111010000101010000111101000011010100010000100110';
for ($i = 0; $i < strlen($fullStringToEncode); $i = $i + 8) {
$eightBits = substr($fullStringToEncode, $i, 8);
$result .= sprintf("%02X", dechex($eightBits));
}
The above php code gives me 020282220222124a2
which is 17 characters long. 上面的php代码给我
020282220222124a2
,它是17个字符长。
This was based on a few days of googling and trying to figure it out. 这是基于几天的谷歌搜索并试图弄清楚的。 Can anyone see where I'm going wrong?
谁能看到我要去哪里错了?
The input string you currently have 0100101000000011111010000101010000111101000011010100010000100110
will be converted to 4A03E8543D0D4426
, you can confirm this with the C#
snippet you have or do the conversion by hand. 您当前具有
0100101000000011111010000101010000111101000011010100010000100110
的输入字符串将转换为4A03E8543D0D4426
,您可以使用您拥有的C#
代码段确认此操作,或手动进行转换。 In order to produce 084a0002d43d0d4426
your input should be 000010000100101000000000000000101101010000111101000011010100010000100110
. 为了产生
084a0002d43d0d4426
您的输入应为000010000100101000000000000000101101010000111101000011010100010000100110
。
That being said, dechex(int $number)
returns a string
containing the hex
representation of $number
. 就是说,
dechex(int $number)
返回一个string
其中包含$number
的hex
表示。 Instead of dechex
, you can use bindec( string $binary_string)
which will return the decimal equivalent of the binary number represented by the $binary_string
argument. 可以使用
bindec( string $binary_string)
代替dechex
,该方法将返回$binary_string
参数表示的二进制数的十进制等效形式。 Modify your code to use bindec
: 修改您的代码以使用
bindec
:
<?php
$fullStringToEncode = '000010000100101000000000000000101101010000111101000011010100010000100110';
$result = '';
for ($i = 0; $i < strlen($fullStringToEncode); $i = $i + 8) {
$eightBits = substr($fullStringToEncode, $i, 8);
$result .= sprintf('%02X', bindec($eightBits));
}
echo $result;
?>
Outputs: 输出:
084A0002D43D0D4426
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.