zj2092 发表于 2018-8-31 13:24:49

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

#!/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 =~//){
  $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";

页: [1]
查看完整版本: perl实现16进制数转换成10进制数