gpt4 book ai didi

PHP 32 位。如何比较字符串和二进制表示形式的 uint64?

转载 作者:可可西里 更新时间:2023-10-31 23:16:58 25 4
gpt4 key购买 nike

先决条件

PHP 5.3.6 32 位(无法迁移到 64 位)。

需要比较 2 个值 uint64(8 字节无符号整数)。其中一个是字符串,另一个是二进制字符串

问题

是否可以将 uint64string 表示形式转换为 8 字节的 array,或者将 array 转换为在 PHP 32 位上使用 uint64 将 8 个字节放入 string

插图

我试过了 base_convert函数来比较 base-2 字符串表示并得到奇怪的结果。我知道字节数组包含与相应字符串相同的 uint64。但我不知道如何确保它们代表相同的数字。

这是带有一些实际值的测试代码来说明问题:

function byte_to_base2string($byte)
{
$byte = base_convert($byte, 10, 2);
$byte = str_pad($byte, 8, '0', STR_PAD_LEFT);
return $byte;
}

function print_info($base10_string1, $bin_string2)
{
$bin_string1 = null; // TODO: how to obtain it?
$base2_string1 = base_convert($base10_string1, 10, 2);
$base2_string1 = str_pad($base2_string1, 64, '0', STR_PAD_LEFT);

$base2_string2 = array_map('byte_to_base2string', $bin_string2);
$base2_string2 = implode('', $base2_string2);
$base10_string2 = base_convert($base2_string2, 2, 10);

echo sprintf("Wrong base-2 string:\n%s\t%s\n", $base10_string1, $base2_string1);
echo sprintf("base-2 string matches $base10_string1, but base-10 string does not\n%s\t%s\n", $base10_string2, $base2_string2);
echo "\n";

// Can't compare because:
// $base2_string1 != $base2_string2
// $base10_string1 != $base10_string2
// $bin_string1 no idea how to convert
}

$strings = [
'288512493108985552',
'288512958990381002',
'288512564016815754'
];

// obtained via unpack('C*', $binaryStr)
$bytes = [
[4, 1, 0, 149, 121, 5, 254, 208],
[4, 1, 1, 1, 241, 183, 239, 202],
[4, 1, 0, 165, 251, 117, 158, 138]
];

array_map('print_info', $strings, $bytes);

输出是:

Wrong base-2 string:
288512493108985552 0000010000000001000000001001010101111001000001011111111011000000
base-2 string matches 288512493108985552, but base-10 string does not
288512493108985526 0000010000000001000000001001010101111001000001011111111011010000

Wrong base-2 string:
288512958990381002 0000010000000001000000010000000111110001101101111110111111000000
base-2 string matches 288512958990381002, but base-10 string does not
288512958990381002 0000010000000001000000010000000111110001101101111110111111001010

Wrong base-2 string:
288512564016815754 0000010000000001000000001010010111111011011101011001111010000000
base-2 string matches 288512564016815754, but base-10 string does not
288512564016815764 0000010000000001000000001010010111111011011101011001111010001010

已更新

找到了解决方案(请参阅下面我的回答),但不确定这是否是最佳方法。还是希望能找到更清晰直接的东西。

最佳答案

好吧,多亏了PHP手册中的一位好心人的评论。 this comment 中的函数完成任务。我找不到路,因为我忘记了 bcmath

所以目前我的工作答案是(使用评论中的 convBase()):

$id1 = "..."; // string representation of uint64 value
$id2 = [...]; // array of bytes

// convert to base-2 string
$id1 = convBase($id1, '0123456789', '01');

// convert each byte to base-2 string (8 chars) and join them
$id2 = implode('', array_map(function($b) {
$b = convBase($b, '0123456789', '01');
$b = str_pad($b, 8, '0', STR_PAD_LEFT);
return $b;
}, $id2));

// pad with leading zeroes to equal length
$len = max(strlen($id1), strlen($id2));
$id1 = str_pad($id1, $len, '0', STR_PAD_LEFT);
$id2 = str_pad($id2, $len, '0', STR_PAD_LEFT);

// Now its OK!
$id1 === $id2;
convBase($id1, '01', '0123456789') === convBase($id2, '01', '0123456789');

我将在此处放置 复制粘贴功能源代码。以防万一 ;)

function convBase($numberInput, $fromBaseInput, $toBaseInput)
{
if ($fromBaseInput == $toBaseInput) return $numberInput;
$fromBase = str_split($fromBaseInput, 1);
$toBase = str_split($toBaseInput, 1);
$number = str_split($numberInput, 1);
$fromLen = strlen($fromBaseInput);
$toLen = strlen($toBaseInput);
$numberLen = strlen($numberInput);
$retval = '';
if ($toBaseInput == '0123456789') {
$retval = 0;
for ($i = 1; $i <= $numberLen; $i++)
$retval = bcadd($retval, bcmul(array_search($number[$i - 1], fromBase), bcpow($fromLen, $numberLen - $i)));
return $retval;
}
if ($fromBaseInput != '0123456789')
$base10 = convBase($numberInput, $fromBaseInput, '0123456789');
else
$base10 = $numberInput;
if ($base10 < strlen($toBaseInput))
return $toBase[$base10];
while ($base10 != '0') {
$retval = $toBase[bcmod($base10, $toLen)] . $retval;
$base10 = bcdiv($base10, $toLen, 0);
}
return $retval;
}

PS:很抱歉我的代码中可能有错别字或错误,我写得很快并且没有测试,只是为了说明解决方案。

关于PHP 32 位。如何比较字符串和二进制表示形式的 uint64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com