Perl 打包/解包/移位

发布于 2024-09-08 17:34:46 字数 987 浏览 5 评论 0原文

我在 Perl 中遇到这个问题已经有几天了,在浏览了无数手册页、perldocs 并在谷歌上搜索了太多搜索词之后,希望这里有人可以帮助我。

我得到了两个代表十六进制值的字符串,即“FFFF”,而不是 Perl 十六进制数字 0xFFFF。给定其中两个字符串,我希望将它们转换为二进制形式,对两者执行按位与,然后获取其输出并检查从 LSB 到 MSB 的每一位。

我现在有两个问题;将十六进制字符串转换为十六进制数字,然后移位 按位与的结果。

为了将十六进制字符串转换为十六进制数字,我尝试了以下方法,当我打印它们进行检查时,这些方法似乎不起作用:

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

$a = sprintf("%H", $a);

使用“打印”来检查其中的每一个都不会显示正确的值,也不显示正确的值。也使用'sprintf'...

第二个问题发生在我执行按位AND之后,我想通过右移1来检查每个位。为了避免上一个问题,我使用了实际的Perl十六进制数字而不是十六进制字符串(0xffff 而不是“ffff”)。如果我尝试如下执行右移:

#Convert from hex number to binary number

$a = sprintf("%B", $a);
$b = sprintf("%B", $b);

$temp = pack("B*", $a) & pack("B*", $b);
$output = unpack("B*", $temp);

此时一切看起来都很好,并且使用“打印”我可以看到 AND 运算的值看起来正确,但是当我尝试如下移动时:

$output = pack("B*", $output);
$output = $output >> 1;
$output = unpack("B*", $output);

结果值 I get 是二进制形式但不正确。

执行此类操作的正确方法是什么?

I've been having this problem in Perl for a few days now, and after scouring countless man pages, perldocs and googling too many search terms, hopefully someone here can help me out.

I am given two strings which represent hex values, i.e. "FFFF", not the Perl hex number 0xFFFF. Given two of these strings, I wish to convert them to binary form, perform a bitwise AND of the two, then take the output of this and examine each bit from LSB to MSB.

I have two problems right now; converting the hex string into a hex number, and shifting
the result of the bitwise AND.

For converting the hex string into a hex number, I've tried the following approaches which don't seem to work when I print them out to examine:

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

$a = sprintf("%H", $a);

Using a 'print' to examine each of these does not show a correct value, nor does using 'sprintf' either...

The second problem I have occurs after I perform a bitwise AND, and I want to examine each bit by shifting right by 1. To avoid the previous problem, I used actual Perl hex numbers instead of hex strings (0xffff instead of "ffff"). If I try to perform a shift right as follows:

#Convert from hex number to binary number

$a = sprintf("%B", $a);
$b = sprintf("%B", $b);

$temp = pack("B*", $a) & pack("B*", $b);
$output = unpack("B*", $temp);

At this point everything looks fine, and using a 'print' I can see that the values of the AND operation look right, but when I try to shift as follows:

$output = pack("B*", $output);
$output = $output >> 1;
$output = unpack("B*", $output);

The resulting value I get is in binary form but not correct.

What is the correct way of performing this kind of operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-09-15 17:34:46

不存在“十六进制数字”这样的东西。数字就是数字,数字的十六进制表示就是一个表示

只需将其转换为数字并使用按位与即可。

my $num = (hex $a) & (hex $b);
print ($num & 1, "\n") while ($num >>= 1)

There's no such thing as a "hex number". A number is a number, a hexadecimal representation of a number is just that - a representation.

Just turn it into a number and use bitwise and.

my $num = (hex $a) & (hex $b);
print ($num & 1, "\n") while ($num >>= 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文