简体   繁体   English

将二进制转换为ASCII,ASCII中的旋转字符串-Java

[英]converting binary to ascii, rotation string in ascii - java

I'm really new in java and now I'm really lost. 我真的是Java新手,现在真的迷路了。 I have to first, convert the binary to its ascii. 我必须首先将二进制文件转换为它的ascii。 Then, create a rotation string (ex: "2L4R6L") of the ascii to get a specific letter. 然后,创建ascii的旋转字符串(例如:“ 2L4R6L”)以获取特定字母。

I'm still on the first part but now I'm really lost. 我仍然在第一部分,但现在我真的迷路了。 I tried the conversion, but when I print it, null is the output. 我尝试了转换,但是当我打印它时,输出为null。 Can you help me point out my mistake, and help me solve this program? 您能帮我指出我的错误并帮助我解决该程序吗?

Here's the methods I created: 这是我创建的方法:

public void setEncryptedMessage(String encryptedMess){
    encryptedMessage = encryptedMess;
    Cipher cph = new Cipher();
    cph.convertBinary(encryptedMessage);
}

public void convertBinary(String encryptedMessage){
    StringTokenizer st = new StringTokenizer(encryptedMessage, '#');
    int convert = Integer.parseInt(st.nextToken(), 2);
    String letter = new Character((char)convert).toString();
    encryptedMessage = letter;
}   

public String getEncryptedMessage(){    
  return encryptedMessage;
}   

this is the main: 这是主要的:

public static void main(String[] args){ 
  Cipher cph=new Cipher();
  String encryptedMessage="1000001#1001001#1011010#1010000#1000110";    
  cph.setEncryptedMessage(encryptedMessage);    
  System.out.println(cph.getEncryptedMessage());
}

摆脱在setEncryptedMessage创建的额外Cipher对象

So you are having trouble converting a Binary in string to ascii form, right? 因此,您无法将字符串中的二进制文件转换为ASCII格式,对吗?
Here! 这里! I found a fix! 我找到了解决方法!

public static void main(String[] args) {
    String encryptedMessage="1000001#1001001#1011010#1010000#1000110"; //BTW one ascii charecter is represented by 8 digits in binary. And here there are 7 digits per charecter...fix that and well moving on...
    String filtered= encryptedMessage.replaceAll("#", "");
    StringBuilder b = new StringBuilder();
    int i = 0;
    String rslt= "";
    while (i + 8 <= filtered.length()) {
    char c = convert(filtered.substring(i, i+8));
    i+=8;
    b.append(c);
    rslt= b.toString();
    }
    System.out.println(rslt);
}
private static char convert(String bs) {
    return (char)Integer.parseInt(bs, 2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM