带有toCharArray()的Java Encode String得到了ArrayIndexOutOfBoundsException

时间:2018-06-18 08:06:38

标签: java encode

所以我的代码是: 但如果我运行它。我得到了java.lang.ArrayIndexOutOfBoundsException 即使我尝试编码一些中文字符或Janpanese字符 喜欢“测试一下” 不要让我添加try catch或OutOfBounds检查

(因为它会改变无法验证的结果)

  

(如果编码“TestThissjkdajld9999kdjalyyy”。它无例外地工作。)

感谢。

    public class Main {  
public static final String l = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public String encode (String fucyo) {
    char[] av = l.toCharArray();
    char[] testssdvx = fucyo.toCharArray();
    int length = testssdvx.length;
    int now = 1;
    String result = "";
    while(now < length-1) {
        synchronized(this) {
        System.out.println(testssdvx[now]);
        System.out.println(testssdvx[now+1]);
        result += av[(testssdvx[now]>>2)];
        result += av[((testssdvx[now]&3)<<4) + (testssdvx[now+1]>>4 )];
        result += av[((testssdvx[now+1]&15)<<2)+ (testssdvx[now+2]>>6)];
        result += av[(testssdvx[now+2] & 63)];
        now +=3;
        }
    }
    System.out.println("Pre:"+result);
    char[] charArray = result.toCharArray(); 
    result = "";
    for (int i=charArray.length-1; i>=0; i--){ 
       result += charArray[i]; 
    } 

    return result;
}


public static void main(String[] args) {    
    System.out.println(new Main().encode("TestThissjkdajldjalksdjalkdjalksdjskladjciiiiiiii78787875ytyyyyyyyyyyyyy"));
    }
}

0 个答案:

没有答案