samyou 发表于 2013-1-26 14:45:35

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 & 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 & 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;    }}
页: [1]
查看完整版本: java下的 UTF-8编码