字符转换

/**
 * 徐士刚
 * 字符转换
 */
import java.io.UnsupportedEncodingException;

public class Tools {

	public static String getEncodingStr(String gbString) {
		StringBuffer unicodeBytes = new StringBuffer();
		for (int i = 0; i < gbString.length(); i++) {
			unicodeBytes.append("&#");
			unicodeBytes.append((int) (gbString.charAt(i)));
			unicodeBytes.append(";");
		}
		return unicodeBytes.toString();
	}
	//对中文进行处理得到相应的ASII码值
	public static String getHexEncoding(String sea) 
    { 
		//char[]arChar=iso2gb(sea).toCharArray();
		char[]arChar=(sea).toCharArray();
        int iValue=0;
        String uStr="";
        for(int i=0;i<arChar.length;i++){
           // iValue=(int)iso2gb(sea).charAt(i);    
        	iValue=(int)(sea).charAt(i);    
            if(iValue<=256){
              // uStr+="&#x00"+Integer.toHexString(iValue)+";";
                uStr+="\\u00"+Integer.toHexString(iValue);
            }else{
               //uStr+="&#x"+Integer.toHexString(iValue)+";";
                uStr+="\\u"+Integer.toHexString(iValue);
            }
        }

        return uStr;
    } 
	
	 public static  String iso2gb(String str) {  
	        try {  
	            str = new String(str.getBytes("ISO-8859-1"), "gb2312");  
	        } catch (Exception e) {  
	            System.out.println("Encoding Error!");  
	        }  
	        return str;  
	    }  
	/**
	 * 3 字符串国标转换为UFT8
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String convertUTFToGBK(String str) {
		if (str == null)
			return "";
		try {
			byte[] bytesStr = str.trim().getBytes("UTF-8");
			return new String(bytesStr, "GBK");
		} catch (Exception ex) {
			return str;
		}
	}

	/**
	 * 3 字符串国标转换为UFT8
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String convertGBKToUTF(String str) {
		if (str == null)
			return "";
		try {
			byte[] bytesStr = str.trim().getBytes("GBK");
			return new String(bytesStr, "UTF-8");
		} catch (Exception ex) {
			return str;
		}
	}

	/**
	 * 3 字符串国标转换为UFT8
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String convertISOToUTF(String str) {
		String markstr = null;
		try {

			if (str == null) {
				markstr = str;
			} else {

				markstr = new String(str.toString().getBytes("ISO-8859-1"),
						"UTF-8");
				// System.out.println(markstr);

			}

		} catch (Exception e) {

		}

		return markstr;
	}
	
	
	/**
	 * 3 字符串国标转换为UFT8
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String convertISOToGBK(String str) {
		if (str == null)
			return "";
		try {
			byte[] bytesStr = str.trim().getBytes("ISO-8859-1");
			return new String(bytesStr, "GBK");
		} catch (Exception ex) {
			return str;
		}
	}

	/**
	 * 3 字符串国标转换为UFT8
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public static String convertISOToUTFGBK(String str) {
		String markstr = null;
		try {

			if (str == null) {
				markstr = str;
			} else {

				markstr = new String(str.toString().getBytes("UTF-8"),
						"GBK");
				// System.out.println(markstr);

			}

		} catch (Exception e) {

		}

		return markstr;
	}
	/**
	 * 判断字符串的编码
	 * 
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s = encode;
				return s;
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}
	
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		System.out.println(getEncodingStr("中"));
		System.out.println(new String("&#20013;".getBytes()));
		String str = "\u6d4b"; 
		//str = str.replaceAll("u", ""); // str = "5927"; 
		System.out.println(str);
		//str = str.replaceAll("\\", ""); // str = "5927"; 
		System.out.println(str);
		//System.out.println((char)Integer.parseInt(str, 16));//printed "大" 

	}

}

 

你可能感兴趣的:(转换)