更改0和1的字符串数组

时间:2014-04-11 08:28:02

标签: java servlets character-encoding

我正在使用只有0和1的字符串数组。现在我想对它进行加密,以便在第i行的数组中,我从该行中获取8个字符,在基数中解析这8个字符2基数然后将其转换为相应的字符值。

这样做的功能如下:

public String[] binaryToText(String[] binary1,int lengthofshares)
{
    String[] encrptedfinally=new String[lengthofshares];
    for(int tt=0;tt<lengthofshares;tt++){
        String ss2="";
        String ss=binary1[tt];
        char mynextChar;

        for(int i = 0; i < ss.length(); i += 8) {
            mynextChar = (char)Integer.parseInt(ss.substring(i, i+8), 2);
            System.out.println();
            ss2 += mynextChar;
        }
        encrptedfinally[tt]=ss2;

    }
return encrptedfinally;

} 

但奇怪的是,当我在使用servlet的web应用程序中使用它时,它会给出不同的错误结果。这可能是什么原因?如何克服它。请帮助

对于字符串数组

String[] binary1 = new String[] {"1000011100011111","01100010","01100011",};

桌面输出:

public static void main(String[] args) {
    // TODO code application logic here

    String[] binary1 = new String[] {"1000011100011111","01100010","01100011",};
    String outt[]=binaryToText(binary1,3);
    for(int i=0;i<3;i++){
        System.out.println(outt[i]);
    }
}

输出:

here is a symbol which am not able to copy,I dont know reason why
b
c

结果的图片是:

enter image description here

Servlet的ProcessRequest方法:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
       String[] binary1 = new String[] {"1000011100011111","01100010","01100011",};
    String outt[]=binaryToText(binary1,3);
    for(int i=0;i<3;i++){
        out.println(outt[i]);
        System.out.println(outt[i]);
    }
    } finally {            
        out.close();
    }
}

仅在网络上输出:

b
c

我的解密部分:

  public String[] textToBinary(String[] alpha,int myK){
  String[] ans=new String[myK+3];
  for(int t=0;t<myK;t++){
        String s=alpha[t];
        byte bytes[]=new byte[s.length()];
        char c[]=s.toCharArray();
        int i;
        for(i=0;i<s.length();i++)
            bytes[i]=(byte)c[i];

        StringBuilder binary = new StringBuilder();
        for (byte b : bytes)
        {
            int val = b;
            for ( i = 0; i < 8; i++)
            {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
        }

        ans[t]=binary.toString();

    }
    return ans;
}

2 个答案:

答案 0 :(得分:0)

正如我预测的那样,你应该为所有ASKII字符获得相同的结果,问题出在其他角色上,并且可能因浏览器而异。所以问题是依赖于浏览器,当您在Web浏览器中更改编码时,它应该显示字符。

答案 1 :(得分:0)

试试这个

public String[] binaryToText1(String[] binary1, int lengthofshares) {
     String[] encrptedfinally = new String[lengthofshares];
     for (int tt = 0; tt < lengthofshares; tt++) {
         String ss2 = "";
         String ss = binary1[tt];
         String mynextChar;
         ss2 =  new java.math.BigInteger(ss, 2).toString(16);
         encrptedfinally[tt] = ss2;
         }
     return encrptedfinally;
}

String[] binary1 = new String[]{"1000011100011111", "01100010", "01100011",};
String outt[] = binaryToText(binary1, 3);
for (int i = 0; i < 3; i++) {
    System.out.println(outt[i]);
    out.println("&#x"+outt[i]+";");
 }