中文编码




char c = '淘'; 中文字
String uncoide = Integer.toHexString(c); 把中文字转成16进制的 unicode 因为是2个字节的
System.out.println(c+"的uncoide编码:\t"+uncoide);


c = '宝';
uncoide = Integer.toHexString(c);
System.out.println(c+"的uncoide编码:\t"+uncoide);


//从uncoide编码转换成10进制
int x = Integer.parseInt(uncoide, 16);
System.out.println(uncoide+"转成10进制:\t"+x);


打印出 23453

宝 的10进制数是 23453
System.out.println(Integer.toBinaryString(23453));


淘宝 两个字的16进制树
String a = "\u6dd8\u5b9d";
System.out.println(a);





package dannytest.charset;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class CharsetTest {

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

Charset charset = Charset.forName("UTF-8");
ByteBuffer byteBuffer = charset.encode("淘宝");
byte[] c1 = byteBuffer.array();
for(byte t : c1){
System.out.println(t);
}

System.out.println(new String(c1, "UTF-8"));
CharBuffer charBuffer = charset.decode(byteBuffer);
String c2 = charBuffer.toString();
System.out.println(c2);

parseByISO_8859_1();
parseByGB2312();
parseByUtf8();

parseByUnicode();





}


public static void parseByUnicode(){
//获得字符的uncoide编码
char c = '淘';
String uncoide = Integer.toHexString(c);
System.out.println(c+"的uncoide编码:\t"+uncoide);

c = '宝';
uncoide = Integer.toHexString(c);
System.out.println(c+"的uncoide编码:\t"+uncoide);


//从uncoide编码转换成10进制
int x = Integer.parseInt(uncoide, 16);
System.out.println(uncoide+"转成10进制:\t"+x);

//从10进制转成uncoide编码
int y = 39118;
System.out.println(y+"转成uncoide编码:\t"+Integer.toHexString(y));


String a = "\u6dd8\u5b9d";


System.out.println(a);
}

public static void parseByUtf8() throws UnsupportedEncodingException{
String tao = "淘";
String bao = "宝";

byte[] taoByte = tao.getBytes("UTF-8");

for(byte t : taoByte){
System.out.println(t);
}

System.out.println("word1 : " + new String(taoByte, "UTF-8"));

byte[] baoByte = bao.getBytes("UTF-8");

for(byte t : baoByte){
System.out.println(t);
}

System.out.println("word2 : " + new String(baoByte, "UTF-8"));

}

public static void parseByISO_8859_1() throws UnsupportedEncodingException{
String tao = "淘";
String bao = "宝";

byte[] taoByte = tao.getBytes("ISO-8859-1");

for(byte t : taoByte){
System.out.println(t);
}

System.out.println("word1 : " + new String(taoByte, "ISO-8859-1"));

byte[] baoByte = bao.getBytes("ISO-8859-1");

for(byte t : baoByte){
System.out.println(t);
}

System.out.println("word2 : " + new String(baoByte, "ISO-8859-1"));

}

public static void parseByGB2312() throws UnsupportedEncodingException{
String tao = "淘";
String bao = "宝";

byte[] taoByte = tao.getBytes("GB2312");

for(byte t : taoByte){
System.out.println(t);
}

System.out.println("word1 : " + new String(taoByte, "GB2312"));

byte[] baoByte = bao.getBytes("GB2312");

for(byte t : baoByte){
System.out.println(t);
}

System.out.println("word2 : " + new String(baoByte, "GB2312"));

}

}

你可能感兴趣的:(java,基础)