Php 反向算法
我使用此算法将MongoID压缩到20个字符,从这里开始:。 但是,我很难将其反转并检索回我的24个字符 有人能给我一些指导吗 我使用的代码:Php 反向算法,php,algorithm,mongodb,Php,Algorithm,Mongodb,我使用此算法将MongoID压缩到20个字符,从这里开始:。 但是,我很难将其反转并检索回我的24个字符 有人能给我一些指导吗 我使用的代码: $padded = str_repeat('0', 20 - strlen($purchase_id)) . $purchase_id; $leastSignificant = base_convert(substr($padded, 14, 10), 32, 16); // will be 8 chars most $middleSignifican
$padded = str_repeat('0', 20 - strlen($purchase_id)) . $purchase_id;
$leastSignificant = base_convert(substr($padded, 14, 10), 32, 16); // will be 8 chars most
$middleSignificant = base_convert(substr($padded, 4, 10), 32, 16); // will be 8 chars most
$highSignificant = base_convert(substr($padded, 0, 4), 32, 16); // will be 4 chars most
// Concatenate, and make sure everything is correctly padded
$result = str_repeat('0', 4 - strlen($highSignificant)) . $highSignificant .
str_repeat('0', 8 - strlen($middleSignificant )) . $middleSignificant .
str_repeat('0', 8 - strlen($leastSignificant )) . $leastSignificant;
我收到错误:警告:str_repeat():第二个参数必须大于或等于0
我得到了与我的原始MongoID相对应的最小重要值和高重要值。但这并不重要。我得到了25个字符而不是24个。得到了
// Pad with 0's to make sure you have 20 chars
$padded = str_repeat('0', 20 - strlen($purchase_id)) . $purchase_id;
$leastSignificant = base_convert(substr($padded, 12, 8), 32, 16); // will be 10 chars most
var_dumped($leastSignificant,false);
$middleSignificant = base_convert(substr($padded, 4, 8), 32, 16); // will be 10 chars most
var_dumped($middleSignificant,false);
$highSignificant = base_convert(substr($padded, 0, 4), 32, 16); // will be 4 chars most
var_dumped($highSignificant,false);
// Concatenate, and make sure everything is correctly padded
$result = str_repeat('0', 4 - strlen($highSignificant)) . $highSignificant .
str_repeat('0', 10 - strlen($middleSignificant )) . $middleSignificant .
str_repeat('0', 10 - strlen($leastSignificant )) . $leastSignificant;