加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数 进制转换 10-16

发布时间:2020-12-14 04:09:09 所属栏目:大数据 来源:网络整理
导读:10-16 ?php ????function?dec2hex($str) ????{ ????????$hex?=?array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); ????????$hexval?=?''; ????????$quotient?=?$str; ????????$divisor?=?$str; ????????$flag?=?true; ????????while

10-16

<?php

????function?dec2hex($str)
????{
????????$hex?=?array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
????????$hexval?=?'';
????????$quotient?=?$str;
????????$divisor?=?$str;
????????$flag?=?true;
????????while($flag)
????????{
????????????$len?=?strlen($divisor);
????????????$pos?=?1;
????????????$quotient?=?0;
????????????$div?=?substr($divisor,?0,?2);
????????????$remainder?=?$div[0];
????????????while($pos?<?$len)
????????????{
????????????????$div?=?$remainder?==?0???$divisor[$pos]?:?$remainder.$divisor[$pos];
????????????????$remainder?=?$div?%?16;
????????????????$quotient?=?$quotient.floor($div/16);
????????????????$pos++;
????????????}
????????????$quotient?=?trim_left_zeros($quotient);
????????????$divisor?=?"$quotient";
????????????$hexval?=?$hex[$remainder].$hexval;
????????????if?(strlen($divisor)<=2)
????????????????if?($divisor<15)
????????????????{
????????????????????$flag?=?false;
????????????????}
????????}
????????$hexval?=?$hex[$quotient].$hexval;
????????$hexval?=?trim_left_zeros($hexval);
????????return?$hexval;
????}
????function?trim_left_zeros($str)
????????$str?=?ltrim($str,?'0');
????????if?(empty($str))
????????????$str?=?'0';
????????return?$str;
????
????$hex?=?dec2hex('10153302696068');
????if($hex%2!=0)?$hex?=?'0'.?$hex;
????$str?=?str_split($hex,2);
????$out?=?'';
????for($i=0;$i<count($str);$i++)
??????$out?=?'x'?.?$str[$i]?.?$out;
????$out?=?$out.'x000';
????echo?$out;
????

?>

16-10

<?php 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,'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; } $btime = microtime(true); echo convBase('c4ca4238a0b923820dcc509a6f75849b','0123456789abcdef','0123456789'); echo microtime(true) - $btime; ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读