Perl数值转换

As always with Perl there is more than one way to do it. Below are a few examples of approaches to making common conversions between number representations. This is intended to be representational rather than exhaustive.

Some of the examples below use the Bit::Vector module from CPAN. The reason you might choose Bit::Vector over the perl built in functions is that it works with numbers of ANY size, that it is optimized for speed on some operations, and for at least some programmers the notation might be familiar.

How do I convert hexadecimal into decimal

Using perl's built in conversion of 0x notation:

 

    $int = 0xDEADBEEF;
    $dec = sprintf("%d", $int);  

 

Using the hex function:

 

    $int = hex("DEADBEEF");
    $dec = sprintf("%d", $int);  

 

Using pack:

 

    $int = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
    $dec = sprintf("%d", $int);  

 

Using the CPAN module Bit::Vector:

 

    use Bit::Vector;
    $vec = Bit::Vector->new_Hex(32, "DEADBEEF");
    $dec = $vec->to_Dec();  

 

How do I convert from decimal to hexadecimal

Using sprint:

 

    $hex = sprintf("%X", 3735928559);  

 

Using unpack

 

    $hex = unpack("H*", pack("N", 3735928559));  

 

Using Bit::Vector

 

    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $hex = $vec->to_Hex();  

 

And Bit::Vector supports odd bit counts:

 

    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(33, 3735928559);
    $vec->Resize(32); # suppress leading 0 if unwanted
    $hex = $vec->to_Hex();  

 

How do I convert from octal to decimal

Using Perl's built in conversion of numbers with leading zeros:

 

    $int = 033653337357; # note the leading 0!
    $dec = sprintf("%d", $int);  

 

Using the oct function:

 

    $int = oct("33653337357");
    $dec = sprintf("%d", $int);  

 

Using Bit::Vector:

 

    use Bit::Vector;
    $vec = Bit::Vector->new(32);
    $vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
    $dec = $vec->to_Dec();  

 

How do I convert from decimal to octal

Using sprintf:

 

    $oct = sprintf("%o", 3735928559);  

 

Using Bit::Vector

 

    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $oct = reverse join('', $vec->Chunk_List_Read(3));  

 

How do I convert from binary to decimal

Perl 5.6 lets you write binary numbers directly with the 0b notation:

 

	$number = 0b10110110;  

 

Using pack and ord

 

    $decimal = ord(pack('B8', '10110110'));  

 

Using pack and unpack for larger strings

 

    $int = unpack("N", pack("B32",
	substr("0" x 32 . "11110101011011011111011101111", -32)));
    $dec = sprintf("%d", $int);

    # substr() is used to left pad a 32 character string with zeros.  

 

Using Bit::Vector:

 

    $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
    $dec = $vec->to_Dec();  

 

How do I convert from decimal to binary

Using unpack;

 

    $bin = unpack("B*", pack("N", 3735928559));  

 

Using Bit::Vector:

 

    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $bin = $vec->to_Bin();  

 

The remaining transformations (e.g. hex -> oct, bin -> hex, etc.) are left as an exercise to the inclined reader.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值