[英]Reverse algorithm
我使用此算法从此处将MongoID压缩为20个字符: 将PHP字符串从24个字符压缩(缩短)为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
$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;
我收到错误消息: Warning: str_repeat(): Second argument has to be greater than or equal to 0
我得到了与原始MongoID正确的MinimumSignificant和highSignificant值。 但不是中间重要的。 我得到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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.