[英]python bytes to bit string
I have value of the type bytes
that need to be converted to BIT STRING
我有需要转换为
BIT STRING
的类型bytes
值
bytes_val = (b'\\x80\\x00', 14)
the bytes in index zero need to be converted to bit string of length as indicated by the second element (14 in this case) and formatted as groups of 8 bits like below.索引零中的字节需要转换为长度由第二个元素(在本例中为 14)指示的位串,并格式化为如下所示的 8 位组。
expected output => '10000000 000000'B
预期输出 =>
'10000000 000000'B
Another example另一个例子
bytes_val2 = (b'\xff\xff\xff\xff\xf0\x00', 45) #=> '11111111 11111111 11111111 11111111 11110000 00000'B
What about some combination of formatting (below with f-string but can be done otherwise), and slicing:格式化的一些组合(下面使用 f-string 但可以通过其他方式完成)和切片怎么样:
def bytes2binstr(b, n=None):
s = ' '.join(f'{x:08b}' for x in b)
return s if n is None else s[:n + n // 8 + (0 if n % 8 else -1)]
If I understood correctly (I am not sure what the B
at the end is supposed to mean), it passes your tests and a couple more:如果我理解正确(我不确定最后的
B
是什么意思),它会通过您的测试和更多测试:
func = bytes2binstr
args = (
(b'\x80\x00', None),
(b'\x80\x00', 14),
(b'\x0f\x00', 14),
(b'\xff\xff\xff\xff\xf0\x00', 16),
(b'\xff\xff\xff\xff\xf0\x00', 22),
(b'\x0f\xff\xff\xff\xf0\x00', 45),
(b'\xff\xff\xff\xff\xf0\x00', 45),
)
for arg in args:
print(arg)
print(repr(func(*arg)))
# (b'\x80\x00', None)
# '10000000 00000000'
# (b'\x80\x00', 14)
# '10000000 000000'
# (b'\x0f\x00', 14)
# '00001111 000000'
# (b'\xff\xff\xff\xff\xf0\x00', 16)
# '11111111 11111111'
# (b'\xff\xff\xff\xff\xf0\x00', 22)
# '11111111 11111111 111111'
# (b'\x0f\xff\xff\xff\xf0\x00', 45)
# '00001111 11111111 11111111 11111111 11110000 00000'
# (b'\xff\xff\xff\xff\xf0\x00', 45)
# '11111111 11111111 11111111 11111111 11110000 00000'
bytes
objectbytes
对象开始b
binary specifier, with some additional formatting: 0
zero fill, 8
minimum lengthb
二进制说明符进行格式化,并带有一些额外的格式: 0
零填充, 8
最小长度' '
as "separator"' '
作为“分隔符”加入(连接)格式化的结果n
was not specified (set to None
), otherwise the result is cropped to n
+ the number of spaces that were added in-between the 8-character groups.n
(设置为None
),则结果将按原样返回,否则结果将被裁剪为n
+ 在 8 个字符组之间添加的空格数。 In the solution above 8
is somewhat hard-coded.在上面的解决方案中,
8
有点硬编码。 If you want it to be a parameter, you may want to look into (possibly a variation of) @kederrac first answer using int.from_bytes()
.如果您希望它成为一个参数,您可能需要使用
int.from_bytes()
来查看(可能是) @kederrac 的第一个答案。 This could look something like:这可能看起来像:
def bytes2binstr_frombytes(b, n=None, k=8):
s = '{x:0{m}b}'.format(m=len(b) * 8, x=int.from_bytes(b, byteorder='big'))[:n]
return ' '.join([s[i:i + k] for i in range(0, len(s), k)])
which gives the same output as above.这给出了与上面相同的输出。
Speedwise, the int.from_bytes()
-based solution is also faster: Speedwise,基于
int.from_bytes()
的解决方案也更快:
for i in range(2, 7):
n = 10 ** i
print(n)
b = b''.join([random.randint(0, 2 ** 8 - 1).to_bytes(1, 'big') for _ in range(n)])
for func in funcs:
print(func.__name__, funcs[0](b, n * 7) == func(b, n * 7))
%timeit func(b, n * 7)
print()
# 100
# bytes2binstr True
# 10000 loops, best of 3: 33.9 µs per loop
# bytes2binstr_frombytes True
# 100000 loops, best of 3: 15.1 µs per loop
# 1000
# bytes2binstr True
# 1000 loops, best of 3: 332 µs per loop
# bytes2binstr_frombytes True
# 10000 loops, best of 3: 134 µs per loop
# 10000
# bytes2binstr True
# 100 loops, best of 3: 3.29 ms per loop
# bytes2binstr_frombytes True
# 1000 loops, best of 3: 1.33 ms per loop
# 100000
# bytes2binstr True
# 10 loops, best of 3: 37.7 ms per loop
# bytes2binstr_frombytes True
# 100 loops, best of 3: 16.7 ms per loop
# 1000000
# bytes2binstr True
# 1 loop, best of 3: 400 ms per loop
# bytes2binstr_frombytes True
# 10 loops, best of 3: 190 ms per loop
you can use:您可以使用:
def bytest_to_bit(by, n):
bi = "{:0{l}b}".format(int.from_bytes(by, byteorder='big'), l=len(by) * 8)[:n]
return ' '.join([bi[i:i + 8] for i in range(0, len(bi), 8)])
bytest_to_bit(b'\xff\xff\xff\xff\xf0\x00', 45)
output:输出:
'11111111 11111111 11111111 11111111 11110000 00000'
steps:脚步:
transform your bytes to an integer using int.from_bytes使用int.from_bytes将您的字节转换为整数
str.format
method can take a binary format spec. str.format
方法可以采用二进制格式规范。
also, you can use a more compact form where each byte is formatted:此外,您可以使用更紧凑的格式,其中每个字节都被格式化:
def bytest_to_bit(by, n):
bi = ' '.join(map('{:08b}'.format, by))
return bi[:n + len(by) - 1].rstrip()
bytest_to_bit(b'\xff\xff\xff\xff\xf0\x00', 45)
test_data = [
(b'\x80\x00', 14),
(b'\xff\xff\xff\xff\xf0\x00', 45),
]
def get_bit_string(bytes_, length) -> str:
output_chars = []
for byte in bytes_:
for _ in range(8):
if length <= 0:
return ''.join(output_chars)
output_chars.append(str(byte >> 7 & 1))
byte <<= 1
length -= 1
output_chars.append(' ')
return ''.join(output_chars)
for data in test_data:
print(get_bit_string(*data))
output:输出:
10000000 000000
11111111 11111111 11111111 11111111 11110000 00000
explanation:解释:
length
: Start from target legnth, and decreasing to 0
. length
:从目标长度开始,递减到0
。if length <= 0: return ...
: If we reached target length, stop and return. if length <= 0: return ...
: 如果我们达到目标长度,停止并返回。''.join(output_chars)
: Make string from list. ''.join(output_chars)
: 从列表中生成字符串。str(byte >> 7 & 1)
byte >> 7
: Shift 7 bits to right(only remains MSB since byte has 8 bits.) byte >> 7
:右移 7 位(由于字节有 8 位,因此仅保留 MSB。)(...) & 1
: Bit-wise and operation. (...) & 1
:按位和操作。 It extracts LSB.byte <<= 1
: Shift 1 bit to left for byte
. byte <<= 1
:为byte
左移 1 位。length -= 1
: Decreasing length
. length -= 1
:减少length
。This is lazy version.这是懒人版。
It neither loads nor processes the entire bytes.它既不加载也不处理整个字节。
This one does halt regardless of input size.无论输入大小如何,这都会停止。
The other solutions may not!其他解决方案可能不会!
I use collections.deque
to build bit string.我使用
collections.deque
来构建位串。
from collections import deque
from itertools import chain, repeat, starmap
import os
def bit_lenght_list(n):
eights, rem = divmod(n, 8)
return chain(repeat(8, eights), (rem,))
def build_bitstring(byte, bit_length):
d = deque("0" * 8, 8)
d.extend(bin(byte)[2:])
return "".join(d)[:bit_length]
def bytes_to_bits(byte_string, bits):
return "{!r}B".format(
" ".join(starmap(build_bitstring, zip(byte_string, bit_lenght_list(bits))))
)
Test;测试;
In [1]: bytes_ = os.urandom(int(1e9))
In [2]: timeit bytes_to_bits(bytes_, 0)
4.21 µs ± 27.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [3]: timeit bytes_to_bits(os.urandom(1), int(1e9))
6.8 µs ± 51 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [4]: bytes_ = os.urandom(6)
In [5]: bytes_
Out[5]: b'\xbf\xd5\x08\xbe$\x01'
In [6]: timeit bytes_to_bits(bytes_, 45) #'10111111 11010101 00001000 10111110 00100100 00000'B
12.3 µs ± 85 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [7]: bytes_to_bits(bytes_, 14)
Out[7]: "'10111111 110101'B"
when you say BIT you mean binary?当你说 BIT 时,你的意思是二进制? I would try
我会尝试
bytes_val = b'\\x80\\x00'
for byte in bytes_val:
value_in_binary = bin(byte)
这给出了没有python的二进制表示0b
的答案:
bit_str = ' '.join(bin(i).replace('0b', '') for i in bytes_val)
This works in Python 3.x:这适用于 Python 3.x:
def to_bin(l):
val, length = l
bit_str = ''.join(bin(i).replace('0b', '') for i in val)
if len(bit_str) < length:
# pad with zeros
return '0'*(length-len(bit_str)) + bit_str
else:
# cut to size
return bit_str[:length]
bytes_val = [b'\x80\x00',14]
print(to_bin(bytes_val))
and this works in 2.x:这适用于 2.x:
def to_bin(l):
val, length = l
bit_str = ''.join(bin(ord(i)).replace('0b', '') for i in val)
if len(bit_str) < length:
# pad with zeros
return '0'*(length-len(bit_str)) + bit_str
else:
# cut to size
return bit_str[:length]
bytes_val = [b'\x80\x00',14]
print(to_bin(bytes_val))
Both produce result 00000100000000
两者都产生结果
00000100000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.