&#x 格式数据转换


import java.io.UnsupportedEncodingException;

/**
* @(#)Convert.java
*
*
*
* @author hcmfys@163.com
* @version 1.00 2013/5/22
*/

public class Convert {

private int hex2oct(char p) throws UnsupportedEncodingException {
char hexData[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
for (int i = 0; i < hexData.length; i++) {
if (Character.toUpperCase(hexData[i]) == p) {
return i;
}
}
throw new UnsupportedEncodingException("Unsupported Hex Encoding");

}
/***
*
* @param p1
* @param p2
* @param p3
* @param p4
* @param charset
* @return
* @throws UnsupportedEncodingException
*/
private String hexString2String(char p1, char p2, char p3, char p4, String charset)
throws UnsupportedEncodingException {

byte first = (byte) (hex2oct(p1) * 16 + hex2oct(p2));
byte second = (byte) (hex2oct(p3) * 16 + hex2oct(p4));
byte[] bytes = new byte[2];
bytes[0] = first;
bytes[1] = second;

return new String(bytes, charset);

}

/**
* 组织专线开通
*
* @throws UnsupportedEncodingException
*/
public String doDecoder(String html, String charset)
throws UnsupportedEncodingException {
int size = html.length();
String result = "";
for (int i = 0; i < size; i++) {
char c = html.charAt(i);
if (c == '&') {
if ((i + 7) < size) {
if (html.charAt(i + 1) == '#' && Character.toUpperCase(html.charAt(i + 2)) == 'X') {
char p1 = html.charAt(i + 3);
char p2 = html.charAt(i + 4);
char p3 = html.charAt(i + 5);
char p4 = html.charAt(i + 6);
String s = hexString2String(p1, p2, p3, p4, charset);
result += s;
i += 7;
}
} else {
result += c;
continue;
}
} else {
result += c;
}
}
return result;
}

public static void main(String[] args) throws Exception {

Convert c = new Convert();
String s = c.doDecoder(
"我们abcd组织专线开fs;&通fs", "utf16");

System.out.println(s);

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值