将char连接成一个字符串

时间:2013-04-17 04:48:22

标签: java

我有这个方法,它接受一个BigInteger,制作另一个BigInteger(通过rsa算法),然后将其转换为二进制,然后将其分解为8个块,我得到该二进制字符串的ascii值。

所有这些工作

但我很难获得从每个二进制字符串中获取的ascii字符并从中创建一个新字符串。我试图使用内置的concat方法,但它似乎没有工作!

    public static String Decrypt( BigInteger ct, BigInteger d, BigInteger mod ){

    String pt = null;
    BigInteger message = ct.modPow(d, mod);     //the decrypted message M but still in BigInteger form
    String plaintext = message.toString(2);

    if( plaintext.length() % 8 != 0 ){
        plaintext = "00000000".substring( plaintext.length() % 8 ) + plaintext;
    }

    String c;
    int charCode = 0;
    for( int i = (plaintext.length()/8) - 1 ; i >= 0; i--){

        charCode = Integer.parseInt((plaintext.substring(i*8, (i*8)+8)) , 2) ;
        c = new Character( (char) charCode).toString();
        System.out.print(c);        // here is where i need something like pt.concat(c) or something like that, I dont really want it printed
    }                               // i just want all of these chars to be put into the string pt 
    System.out.println();
    return pt;
}

正如你在评论中看到的那样我正在谈论的内容,我的意思是通过查看用于连接的API看起来我正在做的是对的,但它只是不起作用!

谢谢你能解释/显示什么错误!

1 个答案:

答案 0 :(得分:4)

1。您应该使用StringBuilder将char附加到。

2。您的代码会创建大量字符串:plaintext.substring(i*8, (i*8)+8))

以下是解决这两个问题的代码。

public static void main(String... args) {

    String plaintext = "";

    { // preparing for test
        String input = "abc";
        for (char ch : input.toCharArray()) {
            String charAs8bits = String.format("%8s",
                    String.valueOf(Integer.toBinaryString(ch))).replace(
                    ' ', '0');
            plaintext = charAs8bits + plaintext;
        }
        System.out.println("plaintext = " + plaintext);
    }

    StringBuilder output = new StringBuilder();
    for (int i = plaintext.length() - 8; i >= 0; i -= 8) {

        boolean isDigit = Character.isDigit(plaintext.charAt(i)) //
                && Character.isDigit(plaintext.charAt(i + 1)) //
                && Character.isDigit(plaintext.charAt(i + 2)) //
                && Character.isDigit(plaintext.charAt(i + 3)) //
                && Character.isDigit(plaintext.charAt(i + 4)) //
                && Character.isDigit(plaintext.charAt(i + 5)) //
                && Character.isDigit(plaintext.charAt(i + 6)) //
                && Character.isDigit(plaintext.charAt(i + 7)) //
        ;

        if (isDigit) {
            int num = (Character.digit(plaintext.charAt(i), 2) << 7) //
                    + (Character.digit(plaintext.charAt(i + 1), 2) << 6) //
                    + (Character.digit(plaintext.charAt(i + 2), 2) << 5) //
                    + (Character.digit(plaintext.charAt(i + 3), 2) << 4) //
                    + (Character.digit(plaintext.charAt(i + 4), 2) << 3) //
                    + (Character.digit(plaintext.charAt(i + 5), 2) << 2) //
                    + (Character.digit(plaintext.charAt(i + 6), 2) << 1) //
                    + (Character.digit(plaintext.charAt(i + 7), 2)) //
            ;
            output.append((char) num);
        }
    }
    System.out.print("output = " + output);
}

输出

plaintext = 011000110110001001100001
output = abc

<强> UPD

我还将for周期更改为对我来说更自然的视图 - 以便i指向“基本”索引。