java下的 UTF-8编码


public class UTFEncode
{
public static String encodeUTF8(String value) {
try {
int strlen = value.length();
StringBuffer out = new StringBuffer();
for (int i = 0; i < strlen; i++) {
char t = value.charAt(i);
int c = 0;
c |= (t & 0xffff);
if (c >= 0 && c < 0x80) {
switch (t) {
case '=':
out.append("%3d");
break;
case ' ':
out.append("%20");
break;
case '+':
out.append("%2b");
break;
case '\'':
out.append("%27");
break;
case '/':
out.append("%2F");
break;
case '.':
out.append("%2E");
break;
case '<':
out.append("%3c");
break;
case '>':
out.append("%3e");
break;
case '#':
out.append("%23");
break;
case '%':
out.append("%25");
break;
case '&':
out.append("%26");
break;
case '{':
out.append("%7b");
break;
case '}':
out.append("%7d");
break;
case '\\':
out.append("%5c");
break;
case '^':
out.append("%5e");
break;
case '~':
out.append("%73");
break;
case '[':
out.append("%5b");
break;
case ']':
out.append("%5d");
break;
default:
out.append(t);
break;
}
}
else if (c > 0x7f && c < 0x800) {
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x1f) | 0xc0) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
} else if (c > 0x7ff && c < 0x10000) {
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 12) & 0x0f) | 0xe0) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x3f) | 0x80) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
} else if (c > 0x00ffff && c < 0xfffff) {
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 18) & 0x07) | 0xf0) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 12) & 0x3f) | 0x80) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x3f) | 0x80) }));
out.append("%");
out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
}
}
return out.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
*
* Convert a byte[] array to readable string format. This makes the "hex"
* readable!
*
* @return result String buffer in String format
*
* @param in
* byte[] buffer to convert to string format
*/
static String byteArrayToHexString(byte in[]) {
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
return null;
String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F" };
StringBuffer out = new StringBuffer(in.length * 2);
while (i < in.length) {
ch = (byte) (in[i] & 0xF0); // Strip off high nibble
ch = (byte) (ch >>> 4);
// shift the bits down
ch = (byte) (ch & 0x0F);
// must do this is high order bit is on!
out.append(pseudo[(int) ch]); // convert the nibble to a String
// Character
ch = (byte) (in[i] & 0x0F); // Strip off low nibble
out.append(pseudo[(int) ch]); // convert the nibble to a String
// Character
i++;
}
String rslt = new String(out);
return rslt;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值