数字到文本解码

时间:2010-12-14 19:48:38

标签: text decode

任何人都可以帮助我提出一种算法或方法,在以下列方式编码时将数字解码为3个字符:

每个元素代表3个字母字符,如以下示例所示:

DOG - > (3 * 26 ^ 2)+(14 * 26)+ 6 = 2398

CAT - > (2 * 26 ^ 2)+(0 * 26)+ 19 = 1371

ZZZ - > (25 * 26 ^ 2)+(25 * 26)+ 25 = 17575

所以说我有7446或3290如何将它们转换为文本?

2 个答案:

答案 0 :(得分:3)

尝试以相反的顺序提取字母:

7446             % 26 = 10
(7446 / 26)      % 26 = 0
(7446 / 26 / 26) % 26 = 11

例如:

1371             % 26 = 19 (T)
(1371 / 26)      % 26 = 0  (A)
(1371 / 26 / 26) % 26 = 2  (C)

CAT

%是指modulo operationx % y为您提供了除法x / y的剩余部分。

答案 1 :(得分:0)

input = 2398
iteration = 1
while input > 0
    character = input % 26
    input = input - character
    input = input / 26
    print character
相关问题