有些网站,特别是有些网站的手机版,喜欢直接输出类似
x6B22; x8FCE; x6765; x5230;Aloong x7684;Java x535A; x5BA2;!
(全部插入了空格避免被转义,下面的代码中也都加了空格)
这类16进制网页编码,虽然我们的浏览器可以显示出正确的文字,但是看源代码的时候就是满眼的乱码了.
于是在需要写处理网页代码的程序就可能遇到转换这些编码的问题.
以下分享两个我转换这种编码的两个例子.
1. 解码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** *//**
* @ClassName: TestDecode
* @Description: 解码16进制网页编码的例子
* @author Aloong
* @date 2010-10-25 上午02:19:59
*
*/
public class TestDecode
{
public static void main(String[] args)
{
String pageCode = "<span> x6B22; x8FCE; x6765; x5230;Aloong x7684;Java x535A; x5BA2;!</span>";
Pattern p;
Matcher m;
//将" x6B22;"转换为"\u6B22"
p = Pattern.compile("[\\d\\w]{4};",Pattern.DOTALL);
m = p.matcher(pageCode);
while (m.find())
{
String group = m.group();
pageCode = pageCode.replaceAll(group, "\\\\u" + group.substring(3, 7).toUpperCase());
}
//去掉html标签的正则表达式
pageCode = pageCode.replaceAll("<[^>]*>", "");
//解码:
pageCode = loadConvert(pageCode);
System.out.println(pageCode);
}
private static String loadConvert(String theString)
{
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;)
{
aChar = theString.charAt(x++);
if (aChar == '\\')
{
aChar = theString.charAt(x++);
if (aChar == 'u')
{
int value = 0;
for (int i = 0; i < 4; i++)
{
aChar = theString.charAt(x++);
switch (aChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else //可选是否转换这些字符
{
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
}
以上程序可以输出:"欢迎来到Aloong的Java博客!"
2. 编码:
/** *//**
* @ClassName: TestEncode
* @Description: 编码16进制网页编码的例子
* @author Aloong
* @date 2010-10-25 上午02:31:58
*
*/
public class TestEncode
{
public static void main(String[] args)
{
String str = "欢迎来到Aloong的Java博客!";
str = toUnicode(str, false);
System.out.println(str);
}
public static String toUnicode(String theString, boolean escapeSpace)
{
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0)
{
bufLen = Integer.MAX_VALUE;
}
StringBuffer outBuffer = new StringBuffer(bufLen);
for (int x = 0; x < len; x++)
{
char aChar = theString.charAt(x);
// Handle common case first, selecting largest block that
// avoids the specials below
if ((aChar > 61) && (aChar < 127))
{
if (aChar == '\\')
{
outBuffer.append('\\');
outBuffer.append('\\');
continue;
}
outBuffer.append(aChar);
continue;
}
switch (aChar)
{
case ' ':
if (x == 0 || escapeSpace)
outBuffer.append('\\');
outBuffer.append(' ');
break;
case '\t':
outBuffer.append('\\');
outBuffer.append('t');
break;
case '\n':
outBuffer.append('\\');
outBuffer.append('n');
break;
case '\r':
outBuffer.append('\\');
outBuffer.append('r');
break;
case '\f':
outBuffer.append('\\');
outBuffer.append('f');
break;
//可选是否转义这些字符
// case '=': // Fall through
// case ':': // Fall through
// case '#': // Fall through
// case '!':
// outBuffer.append('\\'); outBuffer.append(aChar);
// break;
default:
if ((aChar < 0x0020) || (aChar > 0x007e))
{
//添加前缀"",可以改成其他前缀如"\\u"
outBuffer.append("");
outBuffer.append(toHex((aChar >> 12) & 0xF));
outBuffer.append(toHex((aChar >> 8) & 0xF));
outBuffer.append(toHex((aChar >> 4) & 0xF));
outBuffer.append(toHex(aChar & 0xF));
//添加后缀";"
outBuffer.append(';');
} else
{
outBuffer.append(aChar);
}
}
}
return outBuffer.toString();
}
//查询16进制对应表
private static char toHex(int nibble)
{
return hexDigit[(nibble & 0xF)];
}
private static final char[] hexDigit =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F' };
}
以上程序输出:
x6B22; x8FCE; x6765; x5230;Aloong x7684;Java x535A; x5BA2;!
(这里在x的前面都加了空格,不然会被浏览器自动转成文字的.)
以上两个转换代码也是网上找到了,只是自己稍作修改并写了例子.
posted on 2010-10-25 02:48
ApolloDeng 阅读(2007)
评论(1) 编辑 收藏 所属分类:
分享 、
Java 、
Web