How can I get the x LSBs from a string (str) in Python? In the specific I have a 256 bits string consisting in 32 chars each occupying 1 byte, from wich i have to get a "char" string with the 50 Least Significant Bits.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
So here are the ingredients for an approach that works using strings (simple but not the most efficient variant):
ord(x)
yields the number (i.e. essentially the bits) for a char (e.g.ord('A')=65
). Note thatord
expects really an byte-long character (no special signs such as € or similar...)bin(x)[2:]
creates a string representing the numberx
in binary.Thus, we can do (
mystr
holds your string):Note that this approach is surely not the most efficient one due to the heavy string operations that could be replaced by plain integer operations (using bit-shifts and such). However, it is the simplest variant that came to my mind.
If it's only to display, wouldn't it help you?
You can also use
For more information, see here
I think that a possible answer could be in this function:
mystr
holds my stringthis function take 2 LSBs of the -7rd char of
mystr
(these are the 2 MSBs of the 50 LSBs) then take the last 6 characters ofmystr
(these are the 48 LSB of the 50 LSB)Please make me know if I am in error.