2013-02-03 2 views
0

저는 정말 새로운 자바이고 이제는 정말 길어요. 먼저 ascii로 바이너리를 변환해야합니다. 그런 다음 ascii의 회전 문자열 (예 : "2L4R6L")을 만들어 특정 문자를 얻습니다.이진수를 ascii로 변환, 회전 문자열을 ascii - java

저는 아직 첫 번째 부분에 있지만 지금은 정말 길어요. 변환을 시도했지만 인쇄 할 때 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()); 
} 

답변

1

당신은 그래서 당신이 겪고있는 setEncryptedMessage

0

에서 만드는 여분의 Cipher 개체를 제거하십시오 문자열에서 이진수를 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); 
}