4443
Comment:
|
4455
converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 13: | Line 13: |
The closest thing I've found is [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799 ASPN: bit-field manipulation.] | The closest thing I've found is [[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799|ASPN: bit-field manipulation.]] |
Line 171: | Line 171: |
* [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799 ASPN: bit-field manipulation] | * [[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799|ASPN: bit-field manipulation]] |
Line 174: | Line 174: |
* [http://www.python.org/doc/current/lib/module-array.html array module] -- (issued with Python) * [http://www.python.org/doc/current/lib/module-struct.html struct module] -- (issued with Python) * [http://www.python.org/doc/current/lib/module-binascii.html binascii module] -- (issued with Python) * [http://pyserial.sourceforge.net/ pySerial module] -- access the serial port |
* [[http://www.python.org/doc/current/lib/module-array.html|array module]] -- (issued with Python) * [[http://www.python.org/doc/current/lib/module-struct.html|struct module]] -- (issued with Python) * [[http://www.python.org/doc/current/lib/module-binascii.html|binascii module]] -- (issued with Python) * [[http://pyserial.sourceforge.net/|pySerial module]] -- access the serial port |
I'm looking for information on Python bit manipulation, binary manipulation.
It seems that there are no modules for performing Python bit manipulation.
I personally want to be able to:
- Turn "11011000111101..." into bytes, (padded left or right, 0 or 1,) and vice versa.
- Slice ranges of bits
- Rotate bits, addressed by the bit. That is, say: "rotate bits 13-17, wrapping around the edges," or, "rotate bits 13-17, lose bits on the one side, set all new bits to 0."
- Similarly, revert regions of bits, apply logic to regions of bits, etc.,.
- Switch Endianness, with different block sizes.
- Apply operations in block groupings: ex: apply XOR 10101 (5 bits) repeatedly across a field.
The closest thing I've found is ASPN: bit-field manipulation.
I imagine that there are many more manipulations people would like to do with bits.
Manipulations
To integer.
To hex string. Note that you don't need to use x8 bits.
To character. 8 bits max.
Characters to integers, but not to strings of 1's and 0's.
Individual bits.
Transformations Summary
Strings to Integers:
"1011101101": int(str, 2)
"m": ord(str)
"0xdecafbad": int(str, 16) (known to work in Python 2.4)
"decafbad": int(str, 16) (known to work in Python 2.4)
Integers to Strings:
"1011101101": no built-in technique (see below)
"m": chr(str)
"0xdecafbad": hex(val)
"decafbad": "%x" % val
We are still left without a technique for producing binary strings, and decyphering hex strings.
Hex String to Integer
The simplest approach is to use the int type with the base argument.
Another approach to decyphering "0xdecafbad" style hex strings, is to use eval:
However, this could be dangerous, depending on where you're getting your data from.
Here's a function that is safer:
1 def hex_to_integer(h):
2 """Convert a hex string to an integer.
3
4 The hex string can be any length. It can start with an 0x, or not.
5 Unrecognized characters will raise a ValueError.
6
7 This function released into the public domain by it's author, Lion
8 Kimbro.
9 """
10 num = 0 # Resulting integer
11 h = h.lower() # Hex string
12 if h[:2] == "0x":
13 h = h[2:]
14 for c in h: # Hex character
15 num = num * 16
16 if "0" <= c <= "9":
17 num = num + (ord(c) - ord("0"))
18 elif "a" <= c <= "f":
19 num = num + (ord(c) - ord("a"))
20 num = num + 10
21 else:
22 raise ValueError(c)
23 return num
Integer to Bin String
It's high time Python supported binary literals (e.g. 0b10011000) and a bin() function!
Research Links
this is the sort of thing we're looking for:
related modules:
array module -- (issued with Python)
struct module -- (issued with Python)
binascii module -- (issued with Python)
pySerial module -- access the serial port
see also: BitwiseOperators