java字串與二進位的相互轉化

來源:互聯網
上載者:User

標籤:字串數組   article   rtos   csdn   class   div   str1   字串格式化   java   

public class StrBinaryTurn {      // 將Unicode字串轉換成bool型數組      public boolean[] StrToBool(String input) {          boolean[] output = Binstr16ToBool(BinstrToBinstr16(StrToBinstr(input)));          return output;      }        // 將bool型數群組轉換成Unicode字串      public String BoolToStr(boolean[] input) {          String output = BinstrToStr(Binstr16ToBinstr(BoolToBinstr16(input)));          return output;      }        // 將字串轉換成二進位字串,以空格相隔      private String StrToBinstr(String str) {          char[] strChar = str.toCharArray();          String result = "";          for (int i = 0; i < strChar.length; i++) {              result += Integer.toBinaryString(strChar[i]) + " ";          }          return result;      }        // 將二進位字串轉換成Unicode字串      private String BinstrToStr(String binStr) {          String[] tempStr = StrToStrArray(binStr);          char[] tempChar = new char[tempStr.length];          for (int i = 0; i < tempStr.length; i++) {              tempChar[i] = BinstrToChar(tempStr[i]);          }          return String.valueOf(tempChar);      }        // 將二進位字串格式化成全16位帶空格的Binstr      private String BinstrToBinstr16(String input) {          StringBuffer output = new StringBuffer();          String[] tempStr = StrToStrArray(input);          for (int i = 0; i < tempStr.length; i++) {              for (int j = 16 - tempStr[i].length(); j > 0; j--)                  output.append(‘0‘);              output.append(tempStr[i] + " ");          }          return output.toString();      }        // 將全16位帶空格的Binstr轉化成去0首碼的帶空格Binstr      private String Binstr16ToBinstr(String input) {          StringBuffer output = new StringBuffer();          String[] tempStr = StrToStrArray(input);          for (int i = 0; i < tempStr.length; i++) {              for (int j = 0; j < 16; j++) {                  if (tempStr[i].charAt(j) == ‘1‘) {                      output.append(tempStr[i].substring(j) + " ");                      break;                  }                  if (j == 15 && tempStr[i].charAt(j) == ‘0‘)                      output.append("0" + " ");              }          }          return output.toString();      }        // 二進位字串轉化為boolean型數組 輸入16位有空格的Binstr      private boolean[] Binstr16ToBool(String input) {          String[] tempStr = StrToStrArray(input);          boolean[] output = new boolean[tempStr.length * 16];          for (int i = 0, j = 0; i < input.length(); i++, j++)              if (input.charAt(i) == ‘1‘)                  output[j] = true;              else if (input.charAt(i) == ‘0‘)                  output[j] = false;              else                  j--;          return output;      }        // boolean型數組轉化為二進位字串 返回帶0首碼16位有空格的Binstr      private String BoolToBinstr16(boolean[] input) {          StringBuffer output = new StringBuffer();          for (int i = 0; i < input.length; i++) {              if (input[i])                  output.append(‘1‘);              else                  output.append(‘0‘);              if ((i + 1) % 16 == 0)                  output.append(‘ ‘);          }          output.append(‘ ‘);          return output.toString();      }        // 將二進位字串轉換為char      private char BinstrToChar(String binStr) {          int[] temp = BinstrToIntArray(binStr);          int sum = 0;          for (int i = 0; i < temp.length; i++) {              sum += temp[temp.length - 1 - i] << i;          }          return (char) sum;      }        // 將初始二進位字串轉換成字串數組,以空格相隔      private String[] StrToStrArray(String str) {          return str.split(" ");      }        // 將二進位字串轉換成int數組      private int[] BinstrToIntArray(String binStr) {          char[] temp = binStr.toCharArray();          int[] result = new int[temp.length];          for (int i = 0; i < temp.length; i++) {              result[i] = temp[i] - 48;          }          return result;      }    }  

  原文地址:http://blog.csdn.net/q394895302/article/details/50604929

java字串與二進位的相互轉化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.