perl6是否有与Python的bytearray方法等价的方法?

2024-09-28 01:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Rakudoc中找不到bytearray方法或类似的方法。在Python中,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应该提供这种方法还是一些模块?在


Tags: ofthe方法insourcemost定义as
2条回答

brian d foyanswer基本上是正确的。你几乎可以把this code翻译成Perl6

 my $frame = Buf.new; 
 $frame.append(0xA2); 
 $frame.append(0x01); 
 say $frame; # OUTPUT: «Buf:0x<a2 01>␤»

然而,宣言并不相同:

^{pr2}$

在Python中相当于在perl6中

my $bú =  Buf.new('þor'.encode('utf-8')); 
say $bú; # OUTPUT: «Buf:0x<c3 be 6f 72>␤» 

为了使用与错误转换等效的东西,由于perl6处理Unicode规范化的方式不同,这种方法是不同的;您可能必须使用^{}编码。在

然而,对于大多数用途,我想Buf,正如brian d foy所指出的,是正确的。在

我想你在找Buf-一个可变的(通常是无符号的)整数序列。用:bin打开文件将返回Buf。在

相关问题 更多 >

    热门问题