/** * 转换UTF-8 * @param string * @return */ public static String toUtf8String(String string) { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if (c >= 0 && c <= 255) { stringBuffer.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; stringBuffer.append("%" + Integer.toHexString(k).toUpperCase()); } } } return stringBuffer.toString(); }
原文地址:https://www.cnblogs.com/huahuavip/p/10132861.html
时间: 2024-10-20 00:06:01