package small test sample code ideas; Import java.io.CharArrayWriter; Import java.io.PrintWriter; Import java.io.UnsupportedEncodingException; Import java.util.Arrays; public class on the string transcoding { Private static String printByteArray ( byte [] byteArray the) { String javaForm = "signed in the Java representation:" + of Arrays.toString (byteArray the) + ";" ; CharArrayWriter with CAW = new new CharArrayWriter with (); the PrintWriter outs = new new the PrintWriter (CAW); outs.print ( "hexadecimal representation (useful form):" ); outs.print("["); for (int i = 0; i < byteArray.length; i++) { outs.format("0x%02X, ", byteArray[i]); } String hexForm = caw.toString(); hexForm = hexForm.substring(0, hexForm.length() - 2); hexForm = hexForm + "]"; return javaForm + hexForm; } private static void printStringCode(String str) { try { System.err.println("\"" + str + "\"编码如下:"); System.out.println("\t(UTF-8): " + printByteArray(str.getBytes("UTF-8"))); System.out.println("\t(UTF-16): " + printByteArray(str.getBytes("UTF-16"))); System.out.println("\t(GBK): " + printByteArray(str.getBytes("GBK"))); System.out.println("\t(GB2312): " + printByteArray(str.getBytes("GB2312"))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return; } public static void main(String[] args) throws Exception { 0xCE,0xD2 是'我'GBK character encoding ////Because Java compiler recognizes type byte indicates the maximum range of -128 to 127, but are larger than 127 0xCE and 0xD2 // so these two numbers turned negative first deposited into byte type // (because the computer data is stored complement form, so as to keep the negative into a byte array, the data is actually stored binary 0xCE and 0xD2) byte [] = getParam new new byte [] {- (( byte ) (256L - 0xCE)), - (( byte ) (256L - 0xD2 ))}; String value = new new String (getParam, "the ISO-8859-1"); // the copy GBK encoded data as 'I' word to a String object to byte [] value.getBytes = b ( "the ISO-8859-1"); // data value string object copied to the byte array b. string newValue = new new string (b, "GBK");// data array b manner GBK encoding transcoded into intra-coded form (GBK-> UTF-16) a string object System.out.println (newValue); printStringCode ( "I" ); printStringCode ( " they " ); } }
About Java character encoding
Guess you like
Origin www.cnblogs.com/tonekit/p/12131439.html
Recommended
Ranking