java i18n utf_Java国际化(i18n)字符串与Unicode转换

在java中,文本是以Unicode格式内部存储的。 如果输入/输出格式不同,则需要转换。

转换

以下示例将展示将Unicode字符串转换为UTF8字节,以及将UTF8字节转为Unicode字节转换。

文件:IOTester.java -

import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

import java.text.ParseException;

public class I18NTester {

public static void main(String[] args) throws ParseException, UnsupportedEncodingException {

String unicodeString = "\u00C6\u00D8\u00C5" ;

//convert Unicode to UTF8 format

byte[] utf8Bytes = unicodeString.getBytes(Charset.forName("UTF-8"));

printBytes(utf8Bytes, "UTF 8 Bytes");

//convert UTF8 format to Unicode

String converted = new String(utf8Bytes, "UTF8");

byte[] unicodeBytes = converted.getBytes();

printBytes(unicodeBytes, "Unicode Bytes");

}

public static void printBytes(byte[] array, String name) {

for (int k = 0; k < array.length; k++) {

System.out.println(name + "[" + k + "] = " + array[k]);

}

}

}

执行上面示例代码,得到以下结果 -

UTF 8 Bytes[0] = -61

UTF 8 Bytes[1] = -122

UTF 8 Bytes[2] = -61

UTF 8 Bytes[3] = -104

UTF 8 Bytes[4] = -61

UTF 8 Bytes[5] = -123

Unicode Bytes[0] = -58

Unicode Bytes[1] = -40

Unicode Bytes[2] = -59

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值