3097
Comment: How do you safely convert a hex string to a number?
|
3106
|
Deletions are marked like this. | Additions are marked like this. |
Line 88: | Line 88: |
* {{{"1011101101"}}}: {{{int(str, 2)}}} * {{{"m"}}}: {{{ord(str)}}} * {{{"0xdecafbad"}}}: '''unknown technique''' * {{{"decafbad"}}}: '''unknown technique''' |
* {{{"1011101101"}}}: {{{int(str, 2)}}} * {{{"m"}}}: {{{ord(str)}}} * {{{"0xdecafbad"}}}: '''unknown technique''' * {{{"decafbad"}}}: '''unknown technique''' |
Line 94: | Line 94: |
* {{{"1011101101"}}}: '''unknown technique''' * {{{"m"}}}: {{{chr(str)}}} * {{{"0xdecafbad"}}}: {{{hex(val)}}} * {{{"decafbad"}}}: {{{"%x" % val}}} |
* {{{"1011101101"}}}: '''unknown technique''' * {{{"m"}}}: {{{chr(str)}}} * {{{"0xdecafbad"}}}: {{{hex(val)}}} * {{{"decafbad"}}}: {{{"%x" % val}}} |
Line 105: | Line 105: |
>>> eval("0xdecafbad") | >>> eval("0xdecafbad ") |
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 [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799 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": unknown technique
"decafbad": unknown technique
Integers to Strings:
"1011101101": unknown technique
"m": chr(str)
"0xdecafbad": hex(val)
"decafbad": "%x" % val
We are still left without a technique for producing binary strings, and decyphering hex strings.
One 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.
Research Links
this is the sort of thing we're looking for:
[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799 ASPN: bit-field manipulation]
related modules:
[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
see also: BitwiseOperators