java 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法
Posted braveliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法相关的知识,希望对你有一定的参考价值。
public static void main(String [] args) throws UnsupportedEncodingException { int val = 5; System.out.println(Integer.toBinaryString(7)); System.out.println(Integer.toBinaryString(7>>1)); System.out.println(0b111); System.out.println(0b11); System.out.println(Integer.toBinaryString(520)); System.out.println(0b1000001000); String s = "我爱你"; byte [] nn = String.valueOf(s).getBytes("UTF-8"); System.out.println(0xFF); for (int i=0; i< nn.length; i++) { byte ss = nn[i]; System.out.print(ss); } System.out.println(); for (int i=0; i< nn.length; i++) { byte ss = nn[i]; System.out.print(ss & 0xFF); } System.out.println(); for (int i=0; i< nn.length; i++) { byte ss = nn[i]; System.out.print(Integer.toBinaryString((Integer)(ss & 0xFF))); } System.out.println(); //将二进制字符串转换为UTF-8对应的汉字 String binaryStr = "111001101000100010010001111001111000100010110001111001001011110110100000"; String [] binaryArr = new String [binaryStr.length()/8]; for (int i=0; i<binaryStr.length(); i = i + 8) { binaryArr[i/8] = binaryStr.substring(i, i+8); System.out.println(binaryArr[i/8]); } String s1 = String.valueOf(binaryArr); System.out.println(s1); StringBuilder strBuilder = new StringBuilder(); byte [] mm3 = new byte[binaryArr.length]; for(int i=0; i< binaryArr.length; i++) { String m = binaryArr[i]; int nn4 = Integer.parseInt(m, 2); byte mm = (byte)(nn4); mm3[i] = mm; } System.out.print(new String(mm3, Charset.forName("UTF-8"))); }
111
11
7
3
1000001000
520
255
-26-120-111-25-120-79-28-67-96
230136145231136177228189160
111001101000100010010001111001111000100010110001111001001011110110100000
11100110
10001000
10010001
11100111
10001000
10110001
11100100
10111101
10100000
[Ljava.lang.String;@4d76f3f8
我爱你
以上是关于java 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法的主要内容,如果未能解决你的问题,请参考以下文章