我是java的新手,现在我真的迷路了。我必须先将二进制文件转换为ascii。然后,创建ascii的旋转字符串(例如:“2L4R6L”)以获取特定字母。
我还在第一部分,但现在我真的输了。我尝试了转换,但是当我打印它时,null就是输出。你能帮我指出我的错误,并帮我解决这个问题吗?
以下是我创建的方法:
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;
}
这是主要的:
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());
}
答案 0 :(得分:1)
摆脱您在Cipher
setEncryptedMessage
个对象
答案 1 :(得分:0)
所以你在将字符串中的二进制转换为ascii格式时遇到了问题,对吧?这里!我找到了解决办法!
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);
}