将C#代码转换为Java代码

时间:2014-02-25 16:53:40

标签: c# java

我必须将这一小段C#代码转换为Java:

const int AM = 65521;
int GetCCSufix(string a)
{            
    int c = 1, b = 0, d, e;
    var chars = a.ToCharArray();
    for(e =0; e< chars.Length; e ++)
    {
        d = chars[e];
        c = (c + d) % AM;
        b = (b + c) % AM;
    }
    return b << 16 | c;
}

我做到了这一点:

private int getSuffix(String a) {
    int constant = 65521;
    int c = 1;
    int b = 0;
    int d = 0;
    int e = 0;
    for(e = 0; e < a.length(); e++){
        d = a.charAt(e);
        c = (c + d) % constant;
        b = (b + c) % constant;
    }
    return b << 16 | c;
}

但是,这似乎没有给我与C#代码相同的输出。我做错了什么?

1 个答案:

答案 0 :(得分:5)

我对原始代码进行了逐字翻译,看看这是否给出了正确的结果。您使用什么值进行测试,会产生不同的结果?

private static final int AM = 65521;

int getCCSuffix(String a) {
    int c = 1, b = 0, d = 0, e;
    char[] chars = a.toCharArray();
    for (e = 0; e < chars.length; e++) {
        d = chars[e];
        c = (c + d) % AM;
        b = (b + c) % AM;
    }
    return b << 16 | c;
}