UTF8到GBK的相互转换
<%
String str="你好莱坞";
out.print(enCode(str));
out.print("<br/>"+deCode(enCode(str)));
%>
<%!
String enCode(String str)
{
if(str==null)return "";
String hs="";
try
{
byte b[]=str.getBytes("UTF-16");
//System.out.println(byte2hex(b));
for (int n=0;n<b.length;n++)
{
str=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (str.length()==1)
hs=hs+"0"+str;
else
hs=hs+str;
if (n!=b.length-1)hs=hs+"";
}
str= hs.toUpperCase().substring(4);
//System.out.println(str);
char[] chs=str.toCharArray();
str="";
for(int i=0;i<chs.length;i=i+4)
{
str+="&#x"+chs[i]+chs[i+1]+chs[i+2]+chs[i+3]+";";
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return str;
}
String deCode(String str)
{
if (str == null)
{
return "";
}
String temp = "";
String return_temp = "";
try
{
int str_len = str.length() / 7;
for (int i = 1; i <= str_len; i++)
{
temp += str.substring(3 * i + 5 * (i - 1), 7 * i + (i - 1));
}
temp = "FEFF" + temp;
temp = temp.toLowerCase();
int temp_len = temp.length() / 2;
byte mm[] = new byte[temp_len];
String ss[] = new String[temp_len];
for (int i = 0; i < temp_len; i++)
{
String qq = temp.substring(i * 2, i * 2 + 1);
if (!qq.equals("0"))
{
ss[i] = temp.substring(i * 2, i * 2 + 2);
}
else
{
ss[i] = temp.substring(i * 2 + 1, i * 2 + 2);
}
mm[i] = (byte) Integer.parseInt(ss[i], 16);
System.out.println(mm[i]);
}
return_temp = new String(mm, "UTF-16");
}
catch (Exception e)
{
System.out.print(e.getMessage());
}
return return_temp;
}
%>
<%
String str="你好莱坞";
out.print(enCode(str));
out.print("<br/>"+deCode(enCode(str)));
%>
<%!
String enCode(String str)
{
if(str==null)return "";
String hs="";
try
{
byte b[]=str.getBytes("UTF-16");
//System.out.println(byte2hex(b));
for (int n=0;n<b.length;n++)
{
str=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (str.length()==1)
hs=hs+"0"+str;
else
hs=hs+str;
if (n!=b.length-1)hs=hs+"";
}
str= hs.toUpperCase().substring(4);
//System.out.println(str);
char[] chs=str.toCharArray();
str="";
for(int i=0;i<chs.length;i=i+4)
{
str+="&#x"+chs[i]+chs[i+1]+chs[i+2]+chs[i+3]+";";
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return str;
}
String deCode(String str)
{
if (str == null)
{
return "";
}
String temp = "";
String return_temp = "";
try
{
int str_len = str.length() / 7;
for (int i = 1; i <= str_len; i++)
{
temp += str.substring(3 * i + 5 * (i - 1), 7 * i + (i - 1));
}
temp = "FEFF" + temp;
temp = temp.toLowerCase();
int temp_len = temp.length() / 2;
byte mm[] = new byte[temp_len];
String ss[] = new String[temp_len];
for (int i = 0; i < temp_len; i++)
{
String qq = temp.substring(i * 2, i * 2 + 1);
if (!qq.equals("0"))
{
ss[i] = temp.substring(i * 2, i * 2 + 2);
}
else
{
ss[i] = temp.substring(i * 2 + 1, i * 2 + 2);
}
mm[i] = (byte) Integer.parseInt(ss[i], 16);
System.out.println(mm[i]);
}
return_temp = new String(mm, "UTF-16");
}
catch (Exception e)
{
System.out.print(e.getMessage());
}
return return_temp;
}
%>
本文介绍了一种在UTF8和GBK编码之间进行相互转换的方法。通过定义两个函数enCode和deCode,实现了字符串从UTF8编码到GBK编码,再从GBK编码还原回UTF8编码的过程。此方法适用于需要处理不同编码格式的场景。
987

被折叠的 条评论
为什么被折叠?



