首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逆向算法

逆向算法
EN

Stack Overflow用户
提问于 2014-10-12 16:29:38
回答 1查看 54关注 0票数 0

我使用这个算法将我的MongoID压缩为20个字符,从这里:Compress (shorten) PHP string from 24 characters to 20。但是,我在反转它并取回我的24个字符时遇到了麻烦。

谁能给我一些指导。

我使用的代码:

代码语言:javascript
复制
$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

我将leastSignificant和highSignificant的值正确设置为我的原始MongoID。但不是middleSignificant。我得到了25个字符而不是24个。

EN

回答 1

Stack Overflow用户

发布于 2014-10-12 17:03:18

明白了!

代码语言:javascript
复制
// 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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26323274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档