perl数组中的XOR计算器,输出来自xor cal

时间:2017-02-27 17:14:21

标签: perl

如何使用数组

为xor制作基本功能

例如: 我的$ a =“08 F6”; 我的$ b =“4C 6E”;

输出应为= 44 98

喜欢http://xor.pw/

1 个答案:

答案 0 :(得分:4)

在字符串上使用xor:

my $x_hex = '08 F6';
my $y_hex = '4C 6E';

my $x = pack('(H2)*', split(' ', $x_hex));   # my $x = "\x08\xF6";
my $y = pack('(H2)*', split(' ', $y_hex));   # my $y = "\x4C\x6E";

my $z = $x ^ $y;                             # my $z = "\x44\x98";

my $z_hex = join(' ', unpack('(H2)*', $z));  # my $z_hex = "44 98";

在数字上使用xor:

my $x_hex = '08 F6';
my $y_hex = '4C 6E';

my @x = map { hex($_) } split(' ', $x_hex);            # my @x = ( 0x08, 0xF6 );
my @y = map { hex($_) } split(' ', $y_hex);            # my @y = ( 0x4C, 0x6E );

my @z = map { $x[$_] ^ $y[$_] } 0..$#x;                # my @z = ( 0x44, 0x98 );

my $z_hex = join ' ', map { sprintf("%02X", $_) } @z;  # my $z_hex = "44 98";