Loading

Java中字符串转为16进制表示

Java中字符串转为16进制表示

        String str = "鲸";
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder builder = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            builder.append(chars[bit]);
            bit = bs[i] & 0x0f;
            builder.append(chars[bit]);
            builder.append(' ');
        }
        System.out.println(builder.toString().trim());

 

posted @ 2018-05-03 14:58  stono  阅读(12233)  评论(0编辑  收藏  举报