【编码与乱码】(06)---字符的各种值转换
2013-07-01 10:10
337 查看
package example.encoding; /** * The Class ChineseValidator. */ public class CharacterValueConveter { /** * The main method. * * @param args the arguments */ public static void main(String args[]) { CharacterValueConveter conveter = new CharacterValueConveter(); conveter.testConvertion("中文"); } /** * Test convertion. * * @param content the content */ public void testConvertion(String content) { byte[] bytes = content.getBytes(); System.out.println("Original String : " + content); System.out.println(); // Get bytes array System.out.println("Convert string to bytes array : "); for (int i = 0; i < bytes.length; i++) { System.out.println(" byte[" + i + "]=" + bytes[i]); } System.out.println(); // Get each character and unicode System.out.println("Start to convert by character: " + "\n"); for (int j = 0; j < content.length(); j++) { char c = content.charAt(j); printAllValue(c); } } /** * Prints the all value. * * @param c the c */ public void printAllValue(char c) { byte b = (byte) c; int i = (int) c; short s = (short) c; String binaryString = Integer.toBinaryString(i); String hexString = Integer.toHexString(i); StringBuffer output = new StringBuffer(); // Base value output.append("Character : ").append(c).append("\n"); output.append(" byte value : ").append(b).append("\n"); output.append(" int value : ").append(i).append("\n"); output.append(" short value : ").append(s).append("\n"); // Hex value and binary value output.append(" hex value :").append(hexString).append("\n"); output.append(" binary value : ").append(binaryString).append("\n"); // Unicode value in java file & web page with decimal encoding output.append(" unicode value (Base 10): "); output.append("\\u").append(i).append(";").append("\n"); output.append(" unicode value (Base 10 in web page): "); output.append("").append(i).append(";").append("\n"); // Unicode value in java file & web page with hexadecimal encoding output.append(" unicode value (Base 16): "); output.append("\\u").append(hexString).append(";").append("\n"); output.append(" unicode value (Base 16 in web page): "); output.append("").append(hexString).append(";").append("\n"); System.out.println(output.toString()); } }
最终的测试结果如下:
Original String : 中文
Convert string to bytes array :
byte[0]=-42
byte[1]=-48
byte[2]=-50
byte[3]=-60
Start to convert by character:
Character : 中
byte value : 45
int value : 20013
short value : 20013
hex value :4e2d
binary value : 100111000101101
unicode value (Base 10): \u20013;
unicode value (Base 10 in web page): 中
unicode value (Base 16): \u4e2d;
unicode value (Base 16 in web page): e2d;
Character : 文
byte value : -121
int value : 25991
short value : 25991
hex value :6587
binary value : 110010110000111
unicode value (Base 10): \u25991;
unicode value (Base 10 in web page): 文
unicode value (Base 16): \u6587;
unicode value (Base 16 in web page): ᦻ
可以看到在Java中,字符的unicode有两种表示显示:一种是10进制形式,一种是16进制形式。它们可以分别通过:int i = (int)(string.charAt(i))和Integer.toHexString(i);获得。而且在java文件和Web页面,同一个unicode的表示形式是不同。web页面需要用进行转义,在java文件中则使用\u进行转义。
相关文章推荐
- 特殊字符的编码解码,各种编码,乱码各种乱七八糟
- Java字符的编码解码与乱码问题
- 字符集例子-同一字符不同字符集编码不同及导入导出的乱码
- javaweb中文字符编码乱码问题
- 修改Visual Studio 2015 Community默认程序文件编码为GB2312暨解决老版本显示乱码或汉字字符报错的方法
- javadoc时候乱码-编码 GBK 的不可映射字符
- 字符编码详解——彻底理解掌握编码知识,“乱码”不复存在
- 解决全站字符乱码(POST和GET中文编码问题)
- 编程各种字符编码
- OS NSDictionary 输出中文字符”乱码”(Unicode编码)问题?
- 字符,字节和编码(乱码的产生)
- 字符编码-理解跨平台中文乱码
- cmd编译 .java :编码GBK的不可映射字符以及输出乱码 原因以及解决方法
- 关于Java中各种字符编码格式的说明
- javadoc时候乱码-编码 GBK 的不可映射字符
- windows自带记事本导致文本文件(UTF-8编码)开头三个字符乱码问题
- javadoc时候乱码-编码 GBK 的不可映射字符 - wqjsir的专栏 - 博客频道 - CSDN.NET
- 通过“中文”两个字来深度剖析各种不同的编码方式,彻底理解乱码是如何产生的?
- 字符编码详解——彻底理解掌握编码知识,“乱码”不复存在
- 解决 iOS NSDictionary 输出中文字符”乱码”(Unicode编码)问题