php gmp(),PHP: GMP 関数 - Manual

I made a function that can be used for converting numbers to any base you wish.. instead of returning a string of pre-defined index of characters (i.e. 0-9a-z) you could simply make your own of any length using the array of indexes it returns.

I looked around and didn't see anybody made one, I needed one for a simple compression algorithm with only numbers, I've not actually made it yet but this was an initial idea.

// we add an array item $Input%$Base floored and divide $Input by $Base floored.

// repeat until $Input is no longer above 0.functionConvertBase($Input,$Base=10) {$Input=gmp_init($Input);$Result=array();

for($i=0;$i<1||gmp_sign($Input)==1;$i++) {$Result[]=gmp_intval(gmp_mod($Input,$Base));$Input=gmp_div_q($Input,$Base);

}$Result=array_reverse($Result);

return($Result);

}// an example how gmp_strval($.., 36); could be achieved:

// the funny emergency number from The IT Crowd

// (leading zeroes aren't liked in gmp_init though)$Input='1189998819991197253';// our example 36 characters used in gmp_strval($.., 36);$Chars='0123456789abcdefghijklmnopqrstuvwxyz';// count the $Chars so they're all used

// or use your own number less than the length of $Chars$Base=strlen($Chars);// perform$Result=ConvertBase($Input,$Base);// replace the resulting index with the corrosponding characters of the index in $Charsfor($i=0;$i

gmp_strval:  91h7dixfq6h1

BaseConvert: 91h7dixfq6h1

*/?>The example shows a familiar result of course, but the idea of this function was so that you can use whatever base you wish, and display entirely your own output to represent any number of choice.

Also, for those who wish to do bitwise shifting, it's quite simple.. to shift left, just multiply the number by pow(2,x), and to shift right, divide by pow(2,x).

}

functiongmp_shiftr($x,$n) {// shift rightreturn(gmp_div($x,gmp_pow(2,$n)));

}?>

Have fun,

Nitrogen.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值