如何正确编写和读取BitArray对象?

发布于 2025-01-31 09:57:35 字数 588 浏览 2 评论 0原文

我有一个问题。我尝试将我的bitarray对象保存到文件中。之后,我想阅读它并获得相同的bitarray对象我之前保存的内容。但是结果与输入不同。

from bitarray import bitarray

a = bitarray()
a += bitarray('{0:014b}'.format(15))
print(a.to01(), len(a))
with open('j.j', 'wb') as file:
    a.tofile(file)
b = bitarray()    
with open('j.j', 'rb') as file:
    b.fromfile(file)
print(b.to01(), len(b))

输出:

00000000001111 14
0000000000111100 16

我现在看到我的对象是 2字节表示。但是我想获得 14位我保存了。您有什么想法可以做对吗?

I have an issue. I tryed to save my BitArray object into file. After that I want to read it and get the same BitArray object what I saved earlier. But result is not same with input.

from bitarray import bitarray

a = bitarray()
a += bitarray('{0:014b}'.format(15))
print(a.to01(), len(a))
with open('j.j', 'wb') as file:
    a.tofile(file)
b = bitarray()    
with open('j.j', 'rb') as file:
    b.fromfile(file)
print(b.to01(), len(b))

Output:

00000000001111 14
0000000000111100 16

I see my object now is 2-byte representation. But I want to get 14-bit I saved. Do you have any ideas to make it right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2025-02-07 09:57:35

这不是一个很好的解决方案,但确实可以摆脱右边的0。

from bitarray import bitarray

a = bitarray('{0:014b}'.format(15))

print(a.to01(), len(a)) #00000000001111 14

with open('j.j', 'wb') as file:
    a.reverse()
    a.tofile(file)
       
b = bitarray()

with open('j.j', 'rb') as file:
    b.fromfile(file)
    b.reverse()
    
print(b.to01(), len(b)) #0000000000001111 16

您可以跳过逆转和正确的偏移b,但是您必须创建一个动态系统,该系统确切知道要移动多少位。另一个解决方案是首先简单地使用8位。您在这里删除1至7位在这里节省了什么?您不会保存文件中的任何内容。无论如何,这些碎片都将被填充。

This isn't a great solution, but it does get rid of the 0's on the right.

from bitarray import bitarray

a = bitarray('{0:014b}'.format(15))

print(a.to01(), len(a)) #00000000001111 14

with open('j.j', 'wb') as file:
    a.reverse()
    a.tofile(file)
       
b = bitarray()

with open('j.j', 'rb') as file:
    b.fromfile(file)
    b.reverse()
    
print(b.to01(), len(b)) #0000000000001111 16

You could skip the reversals and just right shift b, but you would have to create a dynamic system that knows exactly how many bits to shift by. Another solution is to simply use bits in multiples of 8 in the first place. What are you saving here by removing 1 to 7 bits? You aren't saving anything in the file. Those bits will be padded regardless.

渔村楼浪 2025-02-07 09:57:35

这不是一个很好的解决方案,而是解决方案)

def encode():
   encoded_bits = bitarray()
   ...
   encoded_bits += bitarray('000') # 48:51 slice
   zeroes_at_the_end = 8 - len(encoded_bits) % 8
   if zeroes_at_the_end < 8:
      encoded_bits[48:51] = bitarray('{0:03b}'.format(zeroes_at_the_end)) 

def decode(bites_sequence):
   zeroes_at_the_end = ba2int(bites_sequence[48:51])
   if zeroes_at_the_end != 0:
        del bites_sequence[-zeroes_at_the_end:] 

我只包含数量的零,它将在保存/读取文件后出现,然后轻松删除这些零

It's eather not a great solution, but it's a solution)

def encode():
   encoded_bits = bitarray()
   ...
   encoded_bits += bitarray('000') # 48:51 slice
   zeroes_at_the_end = 8 - len(encoded_bits) % 8
   if zeroes_at_the_end < 8:
      encoded_bits[48:51] = bitarray('{0:03b}'.format(zeroes_at_the_end)) 

def decode(bites_sequence):
   zeroes_at_the_end = ba2int(bites_sequence[48:51])
   if zeroes_at_the_end != 0:
        del bites_sequence[-zeroes_at_the_end:] 

I just contain number of zeroes, which will appear after save/read in files and then easy delete those zeroes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文