使用字母分数进行代码加密

时间:2015-01-30 05:51:01

标签: java encryption

我有一个合乎逻辑的问题。什么是将字符串转换为"得分的最简单方法"在java中,用于加密和解密。这就是我的意思。

A = 1;
B = 2;
C = 3;

等。

我想将整个字符串整理成一个分数,所以" AABC"将= 1 + 1 + 2 + 3 = 7

我意识到我可以将A设置为1,将Z设置为26,但这将是乏味且浪费代码。

2 个答案:

答案 0 :(得分:2)

试试这个,

    char[] charArray = s.toCharArray();

    int total = 0;
    for(char c : charArray)
    {
        total = total + ((int)c) - 64;
    }

    System.out.println("Total : "+total);

答案 1 :(得分:0)

你可以这样做:

public static void main (String[] args) 
    {
        String s = "AABC";
        long score = 0;
        for(int i = 0; i < s.length(); ++i)
        {
            score += s.charAt(i) - 'A' + 1;
            //Basically, you check every index of the string and convert
            //each character into its score and add them.
        }
        System.out.println(score);
    }