package com.chenyang.nse.bussiness.tools.string;

public class BinaryConversion {
   public static String StrToBinstr(char strChar) {
      String result = "";
      result = Integer.toBinaryString(strChar);
      int length = result.length();

      for(int var4 = length % 4 == 0 ? length / 4 : length / 4 + 1; result.length() < var4 * 4; result = "0" + result) {
      }

      return result;
   }

   public static String decimalToBinary(int n) {
      int t = 0;
      int bin = 0;

      for(int r = 0; n != 0; ++t) {
         r = n % 2;
         n /= 2;
         bin = (int)((double)bin + (double)r * Math.pow((double)10.0F, (double)t));
      }

      String str = String.format("%04d", bin);
      return str;
   }

   public static String binaryToDecimal(String n) {
      String res = Integer.valueOf(n, 2).toString();
      return res;
   }

   public static String BinstrToStr(String binStr) {
      char[] tempChar = new char[1];
      tempChar[0] = BinstrToChar(binStr);
      String s = new String(tempChar);
      return s;
   }

   private static char BinstrToChar(String tempStr) {
      int[] temp = BinstrToIntArray(tempStr);
      int sum = 0;

      for(int i = 0; i < temp.length; ++i) {
         sum += temp[temp.length - 1 - i] << i;
      }

      return (char)sum;
   }

   private static 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;
   }

   public static void main(String[] args) {
      String strToBinstr = StrToBinstr('张');
      String binstrToStr = BinstrToStr(strToBinstr);
      System.out.println(binstrToStr);
   }
}
