Unicode编码的byte 数组转为String

Android 学习笔记 (1)

Unicode编码的byte 数组转为String


package com.xbfax.ibor.wxapi;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String s = "12345678";
        System.out.println(s);
        String u = "\\u0031\\u0032\\u0033\\u0034\\u0035\\u0036\\u0037\\u0038";
        System.out.println(u);
        try {
            byte[] bytes1 = s.getBytes("UNICODE");
            byte[] bytes2 = s.getBytes("UTF-16LE");
            byte[] bytes3 = s.getBytes("UTF-16BE");
//            System.out.println(Arrays.toString(bytes1));
//            System.out.println(Arrays.toString(bytes2));
//            System.out.println(Arrays.toString(bytes3));
            System.out.println(Arrays.toString(toHexArr(bytes1)));
            System.out.println(Arrays.toString(toHexArr(bytes2)));
            System.out.println(Arrays.toString(toHexArr(bytes3)));

            System.out.println(new String(bytes1));
            System.out.println(new String(bytes2));
            System.out.println(new String(bytes3));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    static String[] toHexArr(byte[] bytes) {
        String[] hexArr = new String[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            String s = Integer.toHexString(bytes[i]);
            if (s.length() == 1) {
                s = "0" + s;
            }
            s = "0x" + s;
            hexArr[i] = s;
        }
        return hexArr;
    }
}

当直接使用Unicode 作为字符集的时候转换的byte 数组会多出来两个字节,原因是在 Java 中直接使用Unicode 转码时会按照 UTF-16LE 的方式拆分,并加上 BOM(Byte Order Mark)。如果采用 UTF-16 拆分,在 Java 中默认采用带有 BOM 的 UTF-16BE 拆分。so 想去掉多出来的两个字节可以用UTF-16LE或者UTF-16BE来替换直接使用Unicode。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值