Help: C char bits operations to python converting
gangli at msn.com
gangli at msn.com
Wed Sep 27 13:09:05 EDT 2000
In article <hmoA5.965$jk1.66868 at newsc.telia.net>,
"Fredrik Lundh" <effbot at telia.com> wrote:
>
> are you trying to solve a real problem, or an is this just
> some kind of homework?
It is for real ?:). It is part of scurity function for the company I am
working for.
I used a list to skip using the shift operator:
shift4 = [0, 16, 32, 48, 64, 80, 96, 112, -128, -112,
-96, -80, -64, -48, -32, -16, 0, 16, 32, 48,
64, 80, 96, 112, -128, -112
]
>
> (if it's a real problem, note that your C code isn't portable)
>
> anyway, to "cast" a python integer to an 8-bit signed
> integer, mask with 255 and subtract 256 from it if it's
> larger than 127:
>
> def char(value):
> value = value & 255
> if value > 127:
> return value - 256
> return value
>
Thanks for your help. Here my version based on your's:
def char_rshift(n, s=4):
c = (n << s) & 255
if c > 127:
c = c - 256
return c
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list