gpt4 book ai didi

Perl:给定一个任意字符串,你如何提取前 N 位?

转载 作者:行者123 更新时间:2023-12-04 21:51:13 24 4
gpt4 key购买 nike

给定一个字符串 $old_str,我试图将前 N 位(不是字节)提取到 $new_str 中。我一直在阅读包文档和 perlpacktut,但我非常困惑。这是我目前的立场:

my $old_str = "9876543210";                                                     
# Extract the first 5 bits
my $new_str = pack("B5", unpack("B*", $old_str));
printf "%#b | %#b\n", $new_str, $old_str;

这产生:
0b1000 | 0b1001001100101100000001011011101010

但我想要这个:
0b10010 | 0b1001001100101100000001011011101010

最佳答案

你想要内置的 vec:vec

关于Perl:给定一个任意字符串,你如何提取前 N 位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4126108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com