I want to convert strings to bit-fields.Also,convert them to binary and then use. Need help with this..help me ..
Asked
Active
Viewed 402 times
-3
-
Duplicate: http://stackoverflow.com/questions/385572/need-help-typecasting-in-python – S.Lott Dec 22 '08 at 12:35
2 Answers
2
I think the struct module is what you're after.
Example of usage:
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8

Greg
- 316,276
- 54
- 369
- 333
0
they're all binary already... Which language are we talking about?
I'd start by looking at the string as an array of characters and working with each character individually.

Cogsy
- 5,584
- 4
- 35
- 47