编码转换

编码转换!

 

/**
 * @author yshlin
 * @version 1.0
 * E-mail:yshlin1106@126.com
 */
import java.util.StringTokenizer;

public class CodeFuns {
	public CodeFuns() {
	}

	public String toEncoding(String strCh) {
		String strEnch = strCh;
		byte[] byStr;
		try {
			byStr = strEnch.getBytes("iso-8859-1");
			strEnch = new String(byStr);
		} catch (Exception e1) {
			System.out.println("Exception:" + e1);
		}
		return strEnch;
	}

	public String replaceAll(String source, String a, String b) { //得到最终替换的结果
		String cont = processSpace(a, b, source);
		return cont;
	}

	public String processSpace(String from, String to, String source) {
		StringBuffer bf = new StringBuffer();
		StringTokenizer st = new StringTokenizer(source, from, true);
		while (st.hasMoreTokens()) {
			String tmp = st.nextToken();
			if (tmp.equals(from)) {
				bf.append(to);
			} else {
				bf.append(tmp);
			}

		}
		return bf.toString();
	}

	public String toChinese(String strCh) {
		String strChEn = strCh;
		try {
			strChEn = new String(strChEn.getBytes("iso-8859-1"), "GB2312");
		} catch (Exception e2) {
			System.out.println("Exception:" + e2);
		}
		return strChEn;
	}

	/**
	 * 功能:iso-8859-1转换为gb2312
	 * @author renwy
	 * @param qs String
	 * @return String
	 */
	public String iso2gb(String qs) {
		try {
			if (qs == null)
				return "NULL";
			else {
				return new String(qs.getBytes("iso-8859-1"), "gb2312");
			}
		} catch (Exception e) {
			System.err.println("iso2gb error:" + e.getMessage());
			e.printStackTrace();
		}
		return "NULL";
	}

	/**
	 * 功能:gb2312转换为iso-8859-1
	 * @author renwy
	 * @param qs String
	 * @return String
	 */
	public String gb2iso(String gb) {
		try {
			if (gb == null)
				return "NULL";
			else {
				return new String(gb.getBytes("gb2312"), "iso-8859-1");
			}
		} catch (Exception e) {
			System.err.println("gb2iso error:" + e.getMessage());
			e.printStackTrace();
		}
		return "NULL";
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值