初始格式是什么?
my $chr = chr(0b11010010); # A character e.g. from read()
my $bin = '11010010'; # Binary
my $hex = 'D2'; # Hexadecimal
my $num = 0b11010010; # A number.
my $num = 0xD2; # A number.
您想首先将其转换为数字
my $num = ord($chr);
my $num = unpack('C', $chr); # Alternative
my $num = oct("0b$bin");
my $num = hex($hex);
然后你使用班次和面具。
my $channel = ($num >> 0) & 0xF; # Or just: $num & 0xF
my $controller = ($num >> 4) & 0x3;
my $port = ($num >> 6) & 0x3; # Or just: $num >> 6
(您可以使用0b1111
,0b11
和0b11
作为面具。大多数人使用十六进制。)
或者让我们vec
为你找出面具。
my $channel = vec $num, 0, 4;
my $controller = vec $num, 4, 2;
my $port = vec $num, 6, 2;
这是一个示例$controller
:
11010010
>> 4
--------
11010010
& 11
--------
01
(为清楚起见,省略了一些零。)