首页 > 学院 > 开发设计 > 正文

获取utf-8源码(从中文到utf-8)

2019-11-18 13:52:52
字体:
来源:转载
供稿:网友

  获取utf-8源码(从中文到utf-8)

public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append("="+c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.PRintln(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("=" + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表