Perl 6 是否有等同于 Python 的 bytearray 方法?
Does Perl 6 have an equivalent to Python's bytearray method?
我在 Raku doc as in Python. In Python, the bytearray 中找不到 bytearray
方法或类似方法定义如下:
class bytearray([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.
Raku应该提供这个方法还是某个模块?
我认为您正在寻找 Buf - 一个可变的(通常是无符号的)整数序列。使用 :bin
returns a Buf
.
打开文件
brian d foy answer is essentially correct. You can pretty much translate this code 进入 Perl6
my $frame = Buf.new;
$frame.append(0xA2);
$frame.append(0x01);
say $frame; # OUTPUT: «Buf:0x<a2 01>»
但是声明不一样:
bu = bytearray( 'þor', encoding='utf8',errors='replace')
在 Python 中等价于 Perl 6
my $bú = Buf.new('þor'.encode('utf-8'));
say $bú; # OUTPUT: «Buf:0x<c3 be 6f 72>»
并且要使用与错误转换等效的东西,由于 Perl 6 处理 Unicode 规范化的方式不同,该方法也有所不同;您可能必须使用 UTF8 Clean 8
编码。
然而,对于大多数用途,我想 Buf
是正确的,正如 brian d foy 所指出的那样。
我在 Raku doc as in Python. In Python, the bytearray 中找不到 bytearray
方法或类似方法定义如下:
class bytearray([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.
Raku应该提供这个方法还是某个模块?
我认为您正在寻找 Buf - 一个可变的(通常是无符号的)整数序列。使用 :bin
returns a Buf
.
brian d foy answer is essentially correct. You can pretty much translate this code 进入 Perl6
my $frame = Buf.new;
$frame.append(0xA2);
$frame.append(0x01);
say $frame; # OUTPUT: «Buf:0x<a2 01>»
但是声明不一样:
bu = bytearray( 'þor', encoding='utf8',errors='replace')
在 Python 中等价于 Perl 6
my $bú = Buf.new('þor'.encode('utf-8'));
say $bú; # OUTPUT: «Buf:0x<c3 be 6f 72>»
并且要使用与错误转换等效的东西,由于 Perl 6 处理 Unicode 规范化的方式不同,该方法也有所不同;您可能必须使用 UTF8 Clean 8
编码。
然而,对于大多数用途,我想 Buf
是正确的,正如 brian d foy 所指出的那样。