【问题标题】:converting binary to ascii, rotation string in ascii - java将二进制转换为 ascii,在 ascii 中旋转字符串 - java
【发布时间】:2013-02-03 15:42:34
【问题描述】:

我真的是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());
}

【问题讨论】:

    标签: java string binary rotation ascii


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      所以您在将字符串中的二进制转换为 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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-14
        • 1970-01-01
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2011-06-14
        相关资源
        最近更新 更多