码迷,mamicode.com
首页 > 编程语言 > 详细

java 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法

时间:2018-06-24 14:02:06      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:字符   字符串转换   support   yar   span   stringbu   color   throw   div   

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 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法

标签:字符   字符串转换   support   yar   span   stringbu   color   throw   div   

原文地址:https://www.cnblogs.com/braveliuchina/p/9220185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!