字符转码

来源:互联网 发布:mysql密码字典 编辑:程序博客网 时间:2024/06/06 00:00
import java.io.*;class CharCode{public static void main(String []args) throws Exception{//System.getProperty("file.encoding","iso8859-1");//System.getProperties().list(System.out);String strChina="中国";for(int i=0;i<strChina.length();i++){System.out.println(Integer.toHexString(strChina.charAt(i)));}byte [] buf=strChina.getBytes("gb2312");//byte [] buf=strChina.getBytes();//缺省和上面一样。for(int i=0;i<buf.length;i++){System.out.println(Integer.toHexString(buf[i]));}for(int i=0;i<buf.length;i++){System.out.write(buf[i]);}System.out.println();//自动调用flush,刷新缓冲区。//System.out.write(buf,0,4);System.out.println("中国");}}

 

 

import java.io.*;class CharDecode{//Ctrl+C 结束程序public static void main(String []args) throws Exception{System.out.println("Please enter a chinese String:");byte [] buf=new byte[1024];String strInfo=null;int pos=0;int ch=0;while(true){ch=System.in.read();System.out.println(Integer.toHexString(ch));switch(ch){case '\r':break;case '\n':strInfo=new String(buf,0,pos,"gb2312");//按GB2312编码转换成字符串//strInfo=new String(buf,0,pos);//缺省也是GB2312编码。for(int i=0;i<strInfo.length();i++){System.out.println(Integer.toHexString(/*(int)*/strInfo.charAt(i)));//字符串中的每一个字符的Unicode码}break;default:buf[pos++]=(byte)ch;}}}}


 

import java.io.*;class CharDecode{//Ctrl+C 结束程序public static void main(String []args) throws Exception{System.getProperties().put("file.encoding","iso-8859-1");System.out.println("Please enter a chinese String:");byte [] buf=new byte[1024];String strInfo=null;int pos=0;int ch=0;while(true){ch=System.in.read();System.out.println(Integer.toHexString(ch));switch(ch){case '\r':break;case '\n'://strInfo=new String(buf,0,pos,"gb2312");//按GB2312编码转换成字符串strInfo=new String(buf,0,pos);//缺省也是GB2312编码。被第一句改变成iso-8859-1。for(int i=0;i<strInfo.length();i++){System.out.println(Integer.toHexString((int)strInfo.charAt(i)));//字符串中的每一个字符的Unicode码}System.out.println(new String(strInfo.getBytes("iso-8859-1"),"gb2312"));break;default:buf[pos++]=(byte)ch;}}}}