⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chardecode.java

📁 这是清华大学编写的JAVA教材中所有题目的源代码!
💻 JAVA
字号:
import java.io.*;
class CharDecode
{
	public static void main(String args[]) throws Exception
	{
		byte[] buf=new byte[1024];
		int ch=0;
		int pos=0;
		String str=null;
		
		System.out.println("默认输入:");
		while(true)
		{
			ch=System.in.read();
			switch(ch)
			{
				case '\r':
					break;
				case '\n':
					System.out.println();
					System.out.println("buf length="+pos);
					for(int i=0;i<pos;i++)
						System.out.println(Integer.toHexString(buf[i]));
					System.out.println();			


					
					System.out.println("gb2312解码");
					str=new String(buf,0,pos,"gb2312");//用gb2312将字节数组解码成Unicode
					
					//System.out.println("默认解码:"+System.getProperties().getProperty("file.encoding"));
					//str=new String(buf,0,pos);//用默认解码器将字节数组解码成Unicode
					
					//System.out.println("iso8859-1解码");
					//str=new String(buf,0,pos,"iso8859-1");//用iso8859-1将字节数组解码成Unicode
					
					/*System.out.println("iso8859-1编码");
					byte[] tmp=str.getBytes("iso8859-1");//将Unicode再转换成iso8859-1编码,字节数组还原
					System.out.println("gb2312解码");
					str=new String(tmp,"gb2312");//用gb2312将字节数组解码成Unicode
					*/
					System.out.println("str length="+str.length());
					for(int i=0;i<str.length();i++)
						System.out.println(Integer.toHexString((int)str.charAt(i)));
					System.out.println();

					System.out.println("自动将Unicode转换成默认输出:");
					System.out.println(str);
					System.out.println();
					
					
					
					System.out.println("默认输出:");
					for(int i=0;i<pos;i++)
						System.out.write(buf[i]);
					System.out.println();//这句也可改为System.out.flush();
					System.out.println();

					return;
				default:
					buf[pos++]=(byte)ch;
			}
		}
	}
}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -