`
61party
  • 浏览: 1123370 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    perl工程单位转换计算器

    在实际应用中,用户可以通过输入待转换的数值和选择的单位,然后由Perl脚本处理这些信息并返回转换后的结果。CGI部分则负责将这些交互过程封装到Web服务器环境中,使得用户可以通过浏览器访问和使用这个工程单位转换...

    Perl函数全集

    "Perl函数全集"是一个集合了Perl语言中各种内置函数的资源,它相当于Perl程序员的工具箱,帮助开发者快速查找并理解不同函数的功能和用法。这篇概述将深入探讨Perl中的主要函数类别及其应用。 1. **字符串操作函数*...

    Perl函数集锦

    ### Perl函数集锦 #### 概述 Perl作为一种强大的脚本语言,在Web开发、系统管理、文本处理等领域有着广泛的应用。Perl内置了大量的函数,这些函数为开发者提供了丰富的工具箱,帮助他们快速解决问题。本文将详细...

    Perl函数集及内置变量锦集

    Perl进程处理函数、数学函数、字符串处理函数等各类,perl的内置特殊变量还有很多的,例如常用的还有@_、@ARGV、$ARGV、%INC、%ENV等等,有很多,不可能一一列举了,这里给出一个比较有总结性的列表,是chinaunix上...

    perl常用命令_函数集.pdf

    标量转换函数是 Perl 中的一种重要函数,用于将一种数据类型转换为另一种数据类型。下面是 Perl 中的一些常用标量转换函数: 1. int 函数 int 函数是 Perl 中的一个内置函数,用于将一个数字转换为整数。其调用...

    Perl程序如何调用C代码

    1. **参数处理**:XS 负责将 Perl 中的数据类型转换为 C 中对应的数据类型,并在返回时做反向转换。 2. **错误处理**:如果参数不符合预期的要求,XS 会进行错误检测,并可能抛出异常。 3. **函数调用**:XS 允许...

    perl_Perl_

    进行字母大小写的转换,Perl提供了lc(lowercase)和uc(uppercase)函数。若要将所有字符转为小写: ```perl $line = lc($line); ``` 转为大写则替换lc为uc: ```perl $line = uc($line); ``` 处理后的数据可以...

    Perl 6 技术参考手册_PERL技术参考_Perl_

    Perl 6 还有强大的模式匹配功能,允许对集合进行复杂的过滤和转换。 四、数据库操作 Perl 6 支持多种数据库接口,如 DBI(Database Interface)。你可以使用 `connect` 函数建立数据库连接,然后通过 SQL 查询语句...

    Perl编程思想 Perl编程思想

    2. 函数:Perl拥有大量的内置函数,如print、chomp、split等,同时也支持自定义函数。 3. 控制结构:Perl支持if-else、while、for等控制流结构,以及条件运算符和三元运算符。 4. 模块系统:Perl的模块系统(CPAN...

    Perl入门书籍——Perl 24小时

    - **胶水语言**:Perl被称为“胶水语言”,意味着它能有效地将多种技术或程序组件结合在一起,例如将数据库转换为电子表格格式或处理文字文档使其适合Web展示。 - **适应性和兼容性**:Perl能在多种操作系统环境下...

    三天学会Perl

    - **数字和字符串**:讲解Perl中的基本数据类型,包括数值和字符串的表示与操作。 3. **第3学时:控制程序流** - **条件语句与循环**:探讨如何使用if语句和循环结构控制程序执行流程。 4. **第4学时:列表与...

    perl_hash 函数

    在 Perl 中,key 必须是字符串,即使你使用数字作为 key,Perl 也会将其自动转换为字符串。 2. **Value**: 与 key 关联的值,可以是任意类型的数据,包括标量、数组或其他哈希。 3. **Key/Value 对**: 每个 key ...

    Perl语言教程 Perl语言入门

    2. **字符串与数字操作**:Perl自动处理字符串和数字之间的转换,支持基本的算术运算符和字符串连接。例如,`$a + $b` 是加法,`$a . $b` 是字符串拼接。 3. **控制结构**:Perl提供了if语句、while循环、for循环...

    神奇的perl-最佳PERL入门读物.pdf

    4. **Perl的语法**:这部分详细介绍了Perl的基础语法,包括变量、常量、运算符、流程控制语句(如if-else、for、while等)以及函数的使用,帮助初学者掌握编程基础。 5. **哈希与数组**:哈希和数组是Perl中重要的...

    C调用perl脚本程序

    - 使用`perl_get_sv()`函数获取Perl脚本中的变量`line_num`,然后通过`SvIV()`将其转换为C语言中的整型变量。 4. **清理**: - 在脚本执行完毕后,需要调用`perl_destruct()`和`perl_free()`来释放解释器占用的...

    神奇的perl-最佳Perl入门

    - Perl中的子程序(函数)定义使用`sub`关键字。例如: ```perl sub add { my ($a, $b) = @_; return $a + $b; } ``` #### 操作符 - **算术操作符**:`+`、`-`、`*`、`/`、`%`等。 - **自增与自减**:`++`、...

    perl-5.28.0

    例如,它引入了`use v5.28`的开关,用于启用严格的Unicode规范,防止意外的数据转换和编码错误。 2. **语法改进**:Perl 5.28.0引入了新的语法元素,如`given`和`when`,用于简化复杂的条件语句。此外,还增强了`...

Global site tag (gtag.js) - Google Analytics