字符转码

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;
			}
		}
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值