Perl中的位操作

前端之家收集整理的这篇文章主要介绍了Perl中的位操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个属性(32位长),每个位负责特定的功能.我写的Perl脚本应该打开第4位,但保存其他位的先前定义.

我在我的程序中使用:

Sub BitOperationOnAttr

{

my $a="";

MyGetFunc( $a);

$a |= 0x00000008;

MySetFunc( $a);

}

** MyGetFunc / MySetFunc我自己的函数知道读取/修复值.

问题:

>如果使用$a | = 0x00000008;是正确的 ?
>如何从字符串I中通过正则表达式提取十六进制值:例如:

属性:Somestring:value(8 long(0x8))”

解决方法

  1. if usage of $a |= 0x00000008; is right ?

是的,这很好.

  1. how extract hex value by Regular Expression from string I have : For example:

“Attribute: Somestring: value (8 long (0x8))”

我假设你有一个像上面这样的字符串,并希望使用正则表达式来提取“0x8”.在这种情况下,像:

if ($string =~ m/0x([0-9a-fA-F]+)/) {
    $value = hex($1);
} else {
    # string didn't match
}

应该管用.

猜你在找的Perl相关文章