JAVA中将汉字转换为Unicode和Utf-8的两个函数

      Programme 2005-10-19 13:45
static public String StringToUnicode(String text) {
String result = "";
int input;
StringReader isr;
try {
isr = new StringReader(new String(text.getBytes(), "GBK"));
}
catch (UnsupportedEncodingException e) {
return "-1";
}
try {
while ( (input = isr.read()) != -1) {
result = result + "&#x" + Integer.toHexString(input) + ";";

}
}
catch (IOException e) {
return "-2";
}
isr.close();
return result;

}

public static String gb2utf(String inStr) {

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;

System.out.println(ascInt);

System.out.println(Integer.toBinaryString(ascInt));

result = result + "&#x" + Integer.toHexString(ascInt) + ";";

}

return result;

}
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}