Need to add to ord and convert to chr
Stephen Kloder
stephenk at cc.gatech.edu
Sun Nov 5 22:12:05 EST 2000
mchitti at my-deja.com wrote:
> Trying to teach myself python, would appreciate any help.
>
> I have a long string of letters (a caesar cipher) which I chop up,
> convert to ord values and stick into a list. I need to be able to add
> an arbitrary interger to the ord values and then convert back to char.
> This should allow me to brute force the cipher if my logic is good.
>
> TIA
>
If your converting one character at a time, you might want to use
translation tables instead. For example:
>>> from string import uppercase,lowercase,letters,maketrans,translate
>>>
t=maketrans(letters,lowercase[1:]+lowercase[:1]+uppercase[1:]+uppercase[:1])
will create a translation table which shifts every letter one character up
(and wraps around) but does not affect nonnonletters. This can be
implemented by:
>>> translate('abcde',t)
'bcdef'
I'm sure you can see an easy way to generalize this.
--
Stephen Kloder | "I say what it occurs to me to say.
stephenk at cc.gatech.edu | More I cannot say."
Phone 404-874-6584 | -- The Man in the Shack
ICQ #65153895 | be :- think.
More information about the Python-list
mailing list