有些网站,特别是有些网站的手机版,喜欢直接输出类似
x6B22; x8FCE; x6765; x5230; x7684;Java x535A; x5BA2;!
(全部插入了空格避免被转义,下面的代码中也都加了空格)
这类16进制网页编码,虽然我们的浏览器可以显示出正确的文字,但是看源代码的时候就是满眼的乱码了。
于是在需要写处理网页代码的程序就可能遇到转换这些编码的问题。
代码例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 16进制工具类
*/
public class HexadecimalUtils {
public static void main(String[] args) {
String str = "欢迎来到我的Java博客!\n Hello World!";
System.out.println(str);
str = HexadecimalUtils.toUnicode(str);
System.out.println(str);
str = HexadecimalUtils.unicode2String(str);
System.out.println(str);
String pageCode = " x6B22; x8FCE; x6765; x5230; x6211; x7684;Java x535A; x5BA2;!";
String unicode2String = HexadecimalUtils.unicode2String(pageCode);
System.out.println(unicode2String);
}
/**
* 16进制解码
*/
public static String unicode2String(String theString) {
String pageCode = theString;
Pattern p;
Matcher m;
//将"欢"转换为"\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());
}
//将" x6B22;"转换为"\u6B22"
p = Pattern.compile(" x[\\d\\w]{4};", Pattern.DOTALL);
m = p.matcher(pageCode);
while (m.find()) {
String group = m.group();
pageCode = pageCode.replaceAll(group, "\\\\u" + group.substring(4, 8).toUpperCase());
}
//去掉html标签的正则表达式
pageCode = pageCode.replaceAll("<[^>]*>", "");
//解码:
pageCode = loadConvert(pageCode);
return pageCode;
}
/**
* 解码
*/
public static String loadConvert(String theString) {
char aChar;
int len = theString.length();
StringBuilder outBuffer = new StringBuilder(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();
}
/**
* 编码16进制
* @param theString 字符串
* @return 编码16进制
*/
public static String toUnicode(String theString) {
return toUnicode(theString, false);
}
/**
* 编码16进制
* @param theString 字符串
* @param escapeSpace 是否转义空格
* @return 编码16进制
*/
public static String toUnicode(String theString, boolean escapeSpace) {
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = Integer.MAX_VALUE;
}
StringBuilder outBuffer = new StringBuilder(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'};
}
以上程序输出:
欢迎来到我的Java博客!
Hello World!
欢迎来到我的Java博客!\n Hello World!
欢迎来到我的Java博客!
Hello World!
欢迎来到我的Java博客!