Converting grouped hex characters into a bitstring in Perl

六月ゝ 毕业季﹏ 提交于 2019-12-05 13:05:43
my $hex = "7654321076543210";  # can be as long as needed
my $bits = pack("V*", unpack("N*", pack("H*", $hex)));
print unpack("H*", $bits);  #: 1032547610325476
innaM

Consider using the excellent Bit::Vector.

Chas. Owens

Use the hex function to turn the hex string into Perl's internal representation of the number, do your bitwise operations, and use sprintf to turn it back into the hex string:

#!/usr/bin/perl

use strict;
use warnings;

my $hex = "76543210";
my $num = hex $hex;

$num &= 0xFFFF00FF; # Turn off the third byte

my $new_hex = sprintf("%08x", $num);

print "It was $hex and is now $new_hex.\n";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!