看到這個僞 - 應該是平凡實現的:
// you need to define your own range, obviously - it's not at all obvious whether
// e.g. "ź" should be included and that it should come after "z"
array char_range = ['a','á','b','c','č', (...), 'z','ź','ž']
// the text to encode
string plaintext = 'some text here'
// this will contain encoded text
stringbuilder ciphertext = ''
// the classic Caesar Cipher shifts by 3 chars to the right
// to decipher, reverse the sign
int shift_by = 3
// note: character != byte, esp. not in UTF-8 (1 char could be 1 or more bytes)
for each character in plaintext
get character_position of character in char_range // e.g. "a" would return 0
if not in char_range // e.g. spaces and other non-letters
do nothing // drop character
// alternately, you can append it to ciphertext unmodified
continue with next character
add shift_by to character_position
if character_position > char_range.length
character_position modulo char_range.length
if character_position < 0 // useful for decoding
add char_range.length to character_position
get new_character at character_position
append new_character to ciphertext
done
定義 「全範圍」。我不會考慮「ö」,但會包含「ř」 - 顯然,範圍定義取決於您。 – Piskvor 2010-06-14 12:38:30
此外,「á」,「é」,「ö」或「ñ」在任何地方*關閉*都不是ASCII 32-127;取決於字符集,它們可能在任何地方。只要您離開26個基本拉丁字母,該字符集就不會在所選編碼中形成連續的範圍。 – Piskvor 2010-06-14 13:07:19