JAVA字符编码转换类
原创 52cxy 04-03 21:13 阅读数:431

支持转换的有:

import java.io.*;

/**
 * 字符编码转换
 */
public class FormatTrans{
	
	/**
	 * 说明:将输入字符串从"UTF-8"转为"UTF-8"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String UTF8ToUnicode(String strIn) {
		byte[] b;
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("UTF-8"), "iso-8859-1"); //new String(b,"GBK");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strOut;
	}	
	
	/**
	 * 说明:将输入字符串从"iso-8859-1"转为"UTF-8"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String UnicodeToUTF8(String strIn) {
		byte[] b;
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("iso-8859-1"), "UTF-8"); //new String(b,"ISO8859_1");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strOut;
	}

	/**
	 * 说明:将输入字符串从"iso-8859-1"转为"GBK"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String UnicodeToGB(String strIn) {
		byte[] b;
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("iso-8859-1"), "GBK"); //new String(b,"ISO8859_1");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strOut;
	}

	/**
	 * 说明:将输入字符串从"iso-8859-1"转为"GB2312"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String UnicodeToGB2312(String strIn) {
		byte[] b;
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("iso-8859-1"), "GB2312"); //new String(b,"GB2312");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strOut;
	}
	/**
	 * 说明:将输入字符串从"UTF-8"转为"GBK"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String UTF8ToGB(String strIn) {
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("UTF-8"), "GBK");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strOut;
	}
	
	/**
	 * 说明:将输入字符串从"GBK"转为"UTF-8"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String GBToUTF8(String strIn) {
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("GBK"), "UTF-8");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strOut;
	}
	
	
	/**
	 * 说明:将输入字符串从"GBK"转为"iso-8859-1"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String GBToUnicode(String strIn) {
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("GBK"), "iso-8859-1");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strOut;
	}

	/**
	 * 说明:将输入字符串从"GB2312"转为"iso-8859-1"
	 * @param strIn  --待转换字符串
	 * @return 转换后的字符串
	 */
	public static String GB2312ToUnicode(String strIn) {
		String strOut = null;
		if (strIn == null || (strIn.trim()).equals(""))
			return strIn;
		try {
			strOut = new String(strIn.getBytes("GB2312"), "iso-8859-1");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return strOut;
	}

	public static byte[] Base64ToByte(String strIn){
		byte[] bytes = null;
		try {
			bytes =  new sun.misc.BASE64Decoder().decodeBuffer(strIn);
		} catch (IOException e) {
			e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		}
		return bytes;
	}


}


共0条评论
我要评论