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),在别的地方问没结果,小弟初学者,对编码解码一直有点疑惑,望高手详解!!
解决方案 »
- TIJ4 Chapter Access, Exercise 8, page 233
- JAVA中如何查找与文本中匹配的信息
- 如何寻找指定文件的绝对路径,getAbsolutePath()不行!
- java中如何能直接运行 html页面中的JavaScript 的函数
- 如何简单的动态增加二维String数组的长度?
- 求助:如何将byte[]类型的数据转化为二进制读出
- 安装java sdk 1.4时,说磁盘空间不足,竟然不能安装到别的硬盘?
- myeclipse问题
- 请问java怎样调用DLL!
- 我下载了bea的mssql的jdbc驱动程序,请问如何安装?
- 几个方法求优化
- 【求助】jscrollpane显示不出来滚动轴
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;
}
}
}
}