zoukankan      html  css  js  c++  java
  • 2进制转化成字符串

        public static void main(String[] args) {
            System.out.println(StrToBinstr("你好"));
            System.out.println(new StringToBean().BinstrToStr("100111101100000 101100101111101"));
            
        }
           //将Unicode字符串转换成bool型数组
       
       
        //将二进制字符串转换成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);
        }
       
        
       
        //将二进制字符串转换为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;
        }
        private static 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;
        }

  • 相关阅读:
    cf D. Vessels
    cf C. Hamburgers
    zoj 3758 Singles' Day
    zoj 3777 Problem Arrangement
    zoj 3778 Talented Chef
    hdu 5087 Revenge of LIS II
    zoj 3785 What day is that day?
    zoj 3787 Access System
    判断给定图是否存在合法拓扑排序
    树-堆结构练习——合并果子之哈夫曼树
  • 原文地址:https://www.cnblogs.com/aiwoqu/p/4308855.html
Copyright © 2011-2022 走看看