开发手册 欢迎您!
软件开发者资料库

Java内部化 - Unicode Conversion from/to String

Java内部化Unicode Conversion from/to String - 从简单和简单的步骤学习Java内部化从基本到高级概念,包括概述,环境设置,区域设置,区域设置详细信息,显示语言,ResourceBundle,NumberFormat,格式货币,格式百分比,集最小/最大精度,设置舍入模式,解析数字,DecimalFormat,格式编号,特定于区域设置的DecimalFormat,DecimalFormatSymbols,分组数字,DateFormat,格式化日期,格式化时间,格式化日期和时间,SimpleDateFormat,解析日期,特定于区域设置的SimpleDateFormat,DateFormatSymbols, UTC,转换时区,可用时区

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

转换

以下示例将展示Unicode字符串到UTF8 byte []的转换和UTF8 byte []到Unicode byte [].

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] = -61UTF 8 Bytes[1] = -122UTF 8 Bytes[2] = -61UTF 8 Bytes[3] = -104UTF 8 Bytes[4] = -61UTF 8 Bytes[5] = -123Unicode Bytes[0] = -58Unicode Bytes[1] = -40Unicode Bytes[2] = -59