|
发表于 2018-4-10 11:03:28
|
显示全部楼层
本帖最后由 shigure_takimi 于 2018-4-10 11:05 编辑
- # 用Python写的,希望你能参考到。
- # 主要ASCII值之间的转换,以前看别人写的很简单,自己写出来很繁琐。
- # 应该有更好的方法的,但是我不知道怎么搞,期待高人出没。
- def func(text, key = 4, task = 'e'): # 'e' -> encode, other -> decode
- s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
- if task == 'e':
- result = ''
- for i in text:
- if i in s:
- if i.islower():
- result += chr((ord(i)+key-97)%26+97)
- elif i.isupper():
- result += chr((ord(i)+key-65)%26+65)
- else:
- result += i
- else:
- result = ''
- for i in text:
- if i in s:
- if i.islower():
- a = ord(i)-key
- if a < 97:
- result += chr(a+26)
- else:
- result += chr(a)
- elif i.isupper():
- a = ord(i)-key
- if a < 65:
- result += chr(a+26)
- else:
- result += chr(a)
- else:
- result += i
- return result
- print(func('China!'))
- print(func('Glmre!', task = 'd'))
复制代码 |
|