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

perl实现16进制数转换成10进制数

发布时间:2020-12-16 00:35:46 所属栏目:大数据 来源:网络整理
导读:16进制数转换成10进制数,通过Math:BigInt模块实现 #!/usr/bin/perl use warnings; use strict; use Math::BigInt; my %hex_hash = (0=0,1=1,2=2,3=3,4=4,5=5,6=6,??7=7,8=8,9=9,a=10,b=11,c=12,d=13,e=14,f=15); ???? sub hex_to_dec{ ??my $data = shift;
16进制数转换成10进制数,通过Math:BigInt模块实现
#!/usr/bin/perl use warnings; use strict; use Math::BigInt; my %hex_hash = (0=>0,1=>1,2=>2,3=>3,4=>4,5=>5,6=>6,??7=>7,8=>8,9=>9,a=>10,b=>11,c=>12,d=>13,e=>14,f=>15); ???? sub hex_to_dec{ ??my $data = shift; ??my $index = shift; ??--$index; ??my $result = Math::BigInt->new(0); ??my $factor = Math::BigInt->new(1); ??while($index >=2 ){ ????my $digit = substr($data,$index,1); ????if ($digit =~/[a-fA-F]/){ ??????$digit = lc $digit; ??????$digit = $hex_hash{$digit}; ????} ????my $temp = Math::BigInt->new($digit); ????$temp->bmul($factor); ????$result->badd($temp); ????--$index; ????$factor->bmul(16); ??} ??return $result; } print hex_to_dec("0x10",length("0x10")),"n"; print hex_to_dec("0xfff",length("0xfff")),"n";

(编辑:李大同)

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

    推荐文章
      热点阅读