当前位置:   article > 正文

java字符串与二进制的相互转化_java二进制转char

java二进制转char
  1. // 将Unicode字符串转换成bool型数组
  2. public boolean[] StrToBool(String input) {
  3. boolean[] output = Binstr16ToBool(BinstrToBinstr16(StrToBinstr(input)));
  4. return output;
  5. }
  6. // 将bool型数组转换成Unicode字符串
  7. public String BoolToStr(boolean[] input) {
  8. String output = BinstrToStr(Binstr16ToBinstr(BoolToBinstr16(input)));
  9. return output;
  10. }
  11. // 将字符串转换成二进制字符串,以空格相隔
  12. private String StrToBinstr(String str) {
  13. char[] strChar = str.toCharArray();
  14. String result = "";
  15. for (int i = 0; i < strChar.length; i++) {
  16. result += Integer.toBinaryString(strChar[i]) + " ";
  17. }
  18. return result;
  19. }
  20. // 将二进制字符串转换成Unicode字符串
  21. private String BinstrToStr(String binStr) {
  22. String[] tempStr = StrToStrArray(binStr);
  23. char[] tempChar = new char[tempStr.length];
  24. for (int i = 0; i < tempStr.length; i++) {
  25. tempChar[i] = BinstrToChar(tempStr[i]);
  26. }
  27. return String.valueOf(tempChar);
  28. }
  29. // 将二进制字符串格式化成全16位带空格的Binstr
  30. private String BinstrToBinstr16(String input) {
  31. StringBuffer output = new StringBuffer();
  32. String[] tempStr = StrToStrArray(input);
  33. for (int i = 0; i < tempStr.length; i++) {
  34. for (int j = 16 - tempStr[i].length(); j > 0; j--)
  35. output.append('0');
  36. output.append(tempStr[i] + " ");
  37. }
  38. return output.toString();
  39. }
  40. // 将全16位带空格的Binstr转化成去0前缀的带空格Binstr
  41. private String Binstr16ToBinstr(String input) {
  42. StringBuffer output = new StringBuffer();
  43. String[] tempStr = StrToStrArray(input);
  44. for (int i = 0; i < tempStr.length; i++) {
  45. for (int j = 0; j < 16; j++) {
  46. if (tempStr[i].charAt(j) == '1') {
  47. output.append(tempStr[i].substring(j) + " ");
  48. break;
  49. }
  50. if (j == 15 && tempStr[i].charAt(j) == '0')
  51. output.append("0" + " ");
  52. }
  53. }
  54. return output.toString();
  55. }
  56. // 二进制字串转化为boolean型数组 输入16位有空格的Binstr
  57. private boolean[] Binstr16ToBool(String input) {
  58. String[] tempStr = StrToStrArray(input);
  59. boolean[] output = new boolean[tempStr.length * 16];
  60. for (int i = 0, j = 0; i < input.length(); i++, j++)
  61. if (input.charAt(i) == '1')
  62. output[j] = true;
  63. else if (input.charAt(i) == '0')
  64. output[j] = false;
  65. else
  66. j--;
  67. return output;
  68. }
  69. // boolean型数组转化为二进制字串 返回带0前缀16位有空格的Binstr
  70. private String BoolToBinstr16(boolean[] input) {
  71. StringBuffer output = new StringBuffer();
  72. for (int i = 0; i < input.length; i++) {
  73. if (input[i])
  74. output.append('1');
  75. else
  76. output.append('0');
  77. if ((i + 1) % 16 == 0)
  78. output.append(' ');
  79. }
  80. output.append(' ');
  81. return output.toString();
  82. }
  83. // 将二进制字符串转换为char
  84. private char BinstrToChar(String binStr) {
  85. int[] temp = BinstrToIntArray(binStr);
  86. int sum = 0;
  87. for (int i = 0; i < temp.length; i++) {
  88. sum += temp[temp.length - 1 - i] << i;
  89. }
  90. return (char) sum;
  91. }
  92. // 将初始二进制字符串转换成字符串数组,以空格相隔
  93. private String[] StrToStrArray(String str) {
  94. return str.split(" ");
  95. }
  96. // 将二进制字符串转换成int数组
  97. private int[] BinstrToIntArray(String binStr) {
  98. char[] temp = binStr.toCharArray();
  99. int[] result = new int[temp.length];
  100. for (int i = 0; i < temp.length; i++) {
  101. result[i] = temp[i] - 48;
  102. }
  103. return result;
  104. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/617982
推荐阅读
相关标签
  

闽ICP备14008679号