Java 编码例子
发布时间:2020-12-14 22:55:17 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package org.test;import java.io.UnsupportedEncodingException;public class GetCharTest { /** * The main method. * * @param args the arguments
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 package org.test; import java.io.UnsupportedEncodingException; public class GetCharTest { /** * The main method. * * @param args the arguments */ public static void main(String args[]) { String content = "中文"; String defaultEncoding = System.getProperty("file.encoding"); String defaultLnaguage = System.getProperty("user.language"); System.out.println("System default encoding --- " + defaultEncoding); System.out.println("System default language --- " + defaultLnaguage); GetCharTest tester = new GetCharTest(); tester.getCharWithDefaultEncoding(content); tester.getCharWithGivenEncoding(content,"ISO-8859-1"); tester.getCharWithGivenEncoding(content,"GBK"); tester.getCharWithGivenEncoding(content,"UTF-8"); } /** * Gets the char with default encoding. * * @param content the content * * @return the char with default encoding */ public void getCharWithDefaultEncoding(String content) { System.out.println("nGet characters with default encodingn"); printCharArray(content); } /** * Gets the char with given encoding. * * @param content the content * @param encoding the encoding * * @return the char with given encoding */ public void getCharWithGivenEncoding(String content,String encoding) { System.out.println("nGet characters with given encoding : " + encoding + "n"); try { String encodedString = new String(content.getBytes(),encoding); printCharArray(encodedString); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * Prints the char array. * * @param inStr the in str */ public void printCharArray(String inStr) { char[] charArray = inStr.toCharArray(); for (int i = 0; i < inStr.length(); i++) { byte b = (byte) charArray[i]; short s = (short) charArray[i]; String hexB = Integer.toHexString(b).toUpperCase(); String hexS = Integer.toHexString(s).toUpperCase(); StringBuffer sb = new StringBuffer(); // print char sb.append("char["); sb.append(i); sb.append("]='"); sb.append(charArray[i]); sb.append("'t"); // byte value sb.append("byte="); sb.append(b); sb.append(" u"); sb.append(hexB); sb.append('t'); // short value sb.append("short="); sb.append(s); sb.append(" u"); sb.append(hexS); sb.append('t'); // Unicode Block sb.append(Character.UnicodeBlock.of(charArray[i])); System.out.println(sb.toString()); } System.out.println("nCharacters length: " + charArray.length); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |