当前位置: 代码迷 >> Java相关 >> 字符编码
  详细解决方案

字符编码

热度:410   发布时间:2008-03-05 16:47:59.0
字符编码
输入中国怎么照样得到正确编码?
import java.io.*;
public class CharDecoder
{
    public static void main(String [] args) throws Exception
    {
        System.getProperties().put("file.encoding","iso8859-1");
        System.out.println("please enter a Chinese String");
        byte [] buf=new byte[1024];
        int ch=0;  
        int pos=0;
        String strInfo=null;
        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);
                for(int i=0;i<strInfo.length();i++)
                {
                    System.out.println(Integer.toHexString
                        (/*(int)*/strInfo.charAt(i)));
                }
                System.out.println(strInfo);//如输入中国怎么还能输出中国
                return;
            default:
                buf[pos++]=(byte)ch;
        }
        }            
    }
}

运行例子:
D:\>java CharDecoder
please enter a Chinese String
中国
d6
d0
b9
fa
d
a
4e2d
56fd
中国
搜索更多相关的解决方案: 字符  System  String  编码  

----------------解决方案--------------------------------------------------------
  相关解决方案