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)); //问题1
switch(ch)
{
case '\r':
break;
case '\n':
strInfo= new String(buf,0,pos);
for(int i=0;i<strInfo.length();i++) //问题2
{
System.out.println(Integer.toHexString
((int)strInfo.charAt(i)));
}
System.out.println(strInfo);
for(int i=0;i<pos;i++)
System.out.write(buf[i]);
System.out.println();
return;
default:
buf[pos++]=(byte)ch;
}
请问:在这里我看不出怎么个解码,(将本地字符集转换成unicode),在别的地方问没结果,小弟初学者,对编码解码一直有点疑惑,望高手详解!!

解决方案 »

  1.   

    你得代码貌似少IO,另外转码也不是用integer类,是用string下的一个方法
      

  2.   

    嗯,是的,我省略了main(),import也没有加 进来。
      

  3.   

    这是我的全部代码:
    import java.io.*;
    public class CharDecoder
    {
    public static void main(String [] args) throws Exception
    {
            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));// 输出GBK对应的字节数字。
    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)));//输出unicode对应的字节数字。
    }
    System.out.println(strInfo);
    for(int i=0;i<pos;i++)
    System.out.write(buf[i]);
    System.out.println();
    return;
    default:
    buf[pos++]=(byte)ch;
    }
    }
    }
    }