📄 encodeutil.java
字号:
package cn.ineasy.util;
import java.io.UnsupportedEncodingException;
/**
* 编码转换,如GBK到ISO,GBK到Unicode等
*
* @author 王玉玺
* @version 1.0 / 2006-03-16
*/
public class EncodeUtil {
/**
* 把GBK转为Unicode
*
* @param inStr
* gbk编码字符串
* @return Unicode格式字符串
*/
public static String GB2Unicode(String inStr) {
if (inStr == null) {
return null;
}
char temChr;
int ascInt;
int i;
String result = new String("");
if (inStr == null) {
inStr = "";
}
for (i = 0; i < inStr.length(); i++) {
temChr = inStr.charAt(i);
ascInt = temChr + 0;
result = result + "&#x" + Integer.toHexString(ascInt) + ";";
}
return result;
}
/**
* 把GBK转为ISO-8859-1
*
* @param inStr
* gbk编码字符串
* @return ISO-8859-1格式字符串
*/
public static String GBK2ISO(String inStr) {
if (inStr == null) {
return null;
}
try {
return new String(inStr.getBytes("GBK"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public static String GBK2Utf8(String inStr) {
if (inStr == null) {
return null;
}
try {
return new String(inStr.getBytes("GBK"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
/**
* 把ISO-8859-1转为Unicode
*
* @param inStr
* ISO-8859-1编码字符串
* @return Unicode格式字符串
*/
public static String ISO2Unicode(String inStr) {
if (inStr == null) {
return null;
}
try {
inStr = new String(inStr.getBytes("ISO-8859-1"), "GBK");
return GB2Unicode(inStr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
/**
* 把ISO-8859-1转为GBK
*
* @param inStr
* ISO-8859-1编码字符串
* @return GBK格式字符串
*/
public static String ISO2GBK(String inStr) {
if (inStr == null) {
return null;
}
try {
return new String(inStr.getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public static String Utf82GBK(String inStr) {
if (inStr == null) {
return null;
}
try {
return new String(inStr.getBytes("UTF-8"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public static String Utf82ISO(String inStr) {
if (inStr == null) {
return null;
}
try {
return new String(inStr.getBytes("UTF-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
System.out.println(GB2Unicode("测试"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -