Hướng dẫn convert bits to bytes in python - chuyển đổi bit thành byte trong python

I am trying to convert a bit string into a byte string, in Python 3.x. In each byte, bits are filled from high order to low order. The last byte is filled with zeros if necessary. The bit string is initially stored as a "collection" of booleans or integers (0 or 1), and I want to return a "collection" of integers in the range 0-255. By collection, I mean a list or a similar object, but not a character string: for example, the function below returns a generator.

So far, the fastest I am able to get is the following:

def bitsToBytes(a):
    s = i = 0
    for x in a:
        s += s + x
        i += 1
        if i == 8:
            yield s
            s = i = 0
    if i > 0:
        yield s << (8 - i)

I have tried several alternatives: using enumerate, bulding a list instead of a generator, computing s by "(s << 1) | x" instead of the sum, and everything seems to be a bit slower. Since this solution is also one of the shortest and simplest I found, I am rather happy with it.

However, I would like to know if there is a faster solution. Especially, is there a library routine the would do the job much faster, preferably in the standard library?


Example input/output

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]

Here I show the examples with lists. Generators would be fine. However, string would not, and then it would be necessary to convert back and foth between list-like data an string.

bits, bytes, bitstring, and ConstBitStream

Hướng dẫn convert bits to bytes in python - chuyển đổi bit thành byte trong python

Hướng dẫn convert bits to bytes in python - chuyển đổi bit thành byte trong python






bogotobogo.com site search:


bit manipulations

int

 int(x, base=10)

Convert a number or string x to an integer, or return 0 if no arguments are given.

>>> int('00100001', 2)
33

>>> int('0xff',16)
255

>>> int('ff', 16)
255

hex string

To convert to hex string:

>>> "0x%x" % (int('00100001', 2))
'0x21'

char

>>> chr(int('01011110',2))
'^'
>>> int('01000000',2)
64
>>> chr(int('01000000',2))
'@'

>>> int('01110100', 2)
116
>>> chr(int('01110100', 2))
't'
>>> ord('t')
116

bitstring

bitstring classes

The bitstring classes provides four classes:

BitStream and BitArray and their immutable versions ConstBitStream and Bits:

  1. Bits (object): This is the most basic class. It is immutable and so its contents can't be changed after creation.
  2. BitArray (Bits): This adds mutating methods to its base class.
  3. ConstBitStream (Bits): This adds methods and properties to allow the bits to be treated as a stream of bits, with a bit position and reading/parsing methods.
  4. BitStream (BitArray, ConstBitStream): This is the most versative class, having both the bitstream methods and the mutating methods.

hexstring

>>> from bitstring import BitStream, BitArray
>>> c = BitArray(hex='000001b3')
>>> c
BitArray('0x000001b3')
>>> c.hex
'000001b3'

binary string

>>> d = BitArray(bin='0011 00')
>>> d
BitArray('0b001100')
>>> d.bin
'001100'

from int

>>> a = BitArray(uint=45, length=12)
>>> b = BitArray(int=-1, length=7)
>>> a, b
(BitArray('0x02d'), BitArray('0b1111111'))
>>> a.bin
'000000101101'
>>> b.bin
'1111111'

from raw byte

>>> a = BitArray(bytes=b'\x00\x01\x02\xff', length=28, offset=1)
>>> a
BitArray('0x000205f')
>>> a.bin
'0000000000000010000001011111'
>>> a.hex
'000205f'

>>> b = BitArray(bytes=open('video.mp4', 'rb').read())
>>> b
BitArray('0x000000206674797069736f6d0000020069736f6d69736f32617663316d7034310000000866726565048e01fe6d64617400000000001d67640015acd941e08effc0220021c4000003000400000300ca3c58b6580000000668ebe2cb22c0000002d30605ffffcfdc45e9bde6d948b7962cd820d923eeef78323634202d20...') # length=614024968




[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
0

peek

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
1

peek reads from the current bit position pos in the bitstring according to the fmt string or integer and returns the result. The bit position is unchanged.


read

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
2

It reads from current bit position pos in the bitstring according the the format string and returns a single result.

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
3

The sample run below shows it advances 4 bits each time we read a hex number:

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
4

If we read 4 bits and output it as a binary format:

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
5

Output as an unsigned integer as we read in 4 bits each time and advance:

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
6

pos and bitpos

pos/bitpos is a read and write property for setting and getting the current bit position in the bitstring. Can be set to any value from 0 to len.

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
7

The following code reads in video.mp4 one byte at a time, convert it to character, and then puts it into a list. It reads only the first 180 bytes.

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
8

The output looks like this:

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]
9



more




Python tutorial


Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...




Làm thế nào để bạn chuyển đổi bit thành byte?

Để chuyển đổi từ các bit thành byte, chỉ cần chia số bit cho 8. Ví dụ: 256 bit bằng 256 /8 = 32 byte.divide the number of bits by 8. For example, 256 bits are equal to 256 / 8 = 32 bytes.

Byte () làm gì trong Python?

Hàm python byte () hàm byte () trả về đối tượng byte.Nó có thể chuyển đổi các đối tượng thành các đối tượng byte hoặc tạo đối tượng byte trống của kích thước được chỉ định.returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size.

Làm thế nào để bạn chuyển đổi một số thành một byte trong Python?

Giá trị INT có thể được chuyển đổi thành byte bằng cách sử dụng phương thức int.to_bytes ().int. to_bytes().

Kích thước byte trong Python là gì?

Hàm byte () chuyển đổi một đối tượng (như danh sách, chuỗi, số nguyên, v.v.) thành một đối tượng byte bất biến trong phạm vi từ 0 đến 256. Nếu không có đối tượng nào được cung cấp cho phương thức byte (), nó có thể tạo ra mộtCác byte trống đối tượng của một kích thước cụ thể.0 to 256. If no object is provided to the bytes() method, it can generate an empty bytes object of a specified size.