WEB开发网
开发学院WEB开发Jsp 获取utf-8源码(从中文到utf-8) 阅读

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

 2008-01-05 09:32:21 来源:WEB开发网   
核心提示:获取utf-8源码(从中文到utf-8) public static String toUtf8String(String s) { StringBuffer sb = new StringBuffer(); for (int i=0;i<s.length();i++) { char c

  获取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();
}

Tags:获取 utf 源码

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接