【问题标题】:Is there a quick function to encode/decode strings in Android using full percent encoding?是否有使用完整百分比编码在 Android 中编码/解码字符串的快速功能?
【发布时间】:2011-08-11 16:42:07
【问题描述】:

我想在我的 Android 应用程序中对某些字符串操作使用百分比值编码/解码。我不想使用 URI 编码/解码函数对,因为我想编码 每个 字符,而不仅仅是在编码 URI 以发出 Web 请求时编码的字符。 Android 库或 Java 库中是否有内置函数可以做到这一点?

--罗施勒

【问题讨论】:

    标签: java android decode encode percent-encoding


    【解决方案1】:

    API 中没有内置任何内容可以直接执行此操作,但它非常简单。最好使用特定的字符编码(如 UTF-8)将字符转换为字节。这应该可以解决编码问题:

    static final String digits = "0123456789ABCDEF";
    
    static void convert(String s, StringBuffer buf, String enc)
            throws UnsupportedEncodingException {
        byte[] bytes = s.getBytes(enc);
        for (int j = 0; j < bytes.length; j++) {
            buf.append('%');
            buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
            buf.append(digits.charAt(bytes[j] & 0xf));
        }
    }
    

    哦,是的,你也要求解码:

    static String decode(String s, String enc)
            throws UnsupportedEncodingException {
        StringBuffer result = new StringBuffer(s.length());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for (int i = 0; i < s.length();) {
            char c = s.charAt(i);
            if (c == '%') {
                out.reset();
                do {
                    if (i + 2 >= s.length()) {
                        throw new IllegalArgumentException(
                            "Incomplete trailing escape (%) pattern at " + i);
                    }
                    int d1 = Character.digit(s.charAt(i + 1), 16);
                    int d2 = Character.digit(s.charAt(i + 2), 16);
                    if (d1 == -1 || d2 == -1) {
                        throw new IllegalArgumentException(
                            "Illegal characters in escape (%) pattern at " + i
                            + ": " + s.substring(i, i+3));
                    }
                    out.write((byte) ((d1 << 4) + d2));
                    i += 3;
                } while (i < s.length() && s.charAt(i) == '%');
                result.append(out.toString(enc));
                continue;
            } else {
                result.append(c);
            }
            i++;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这很简单,不需要库函数:

      public static String escapeString(String input) {
          String output = "";
      
          for (byte b : input.getBytes()) output += String.format("%%%02x", b);
          return output;
      }
      
      public static String unescapeString(String input) {
          String output = "";
      
          for (String hex: input.split("%")) if (!"".equals(hex)) output += (char)Integer.parseInt(hex, 16);
          return output;
      }
      
      public static String unescapeMultiByteString(String input, String charset) {
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          String result = null;
      
          for (String hex: input.split("%")) if (!"".equals(hex)) output.write(Integer.parseInt(hex, 16));
          try { result = new String(output.toByteArray(), charset); }
          catch (Exception e) {}
          return result;
      }
      

      【讨论】:

      • 这可能会产生%H,只有一位数。 %HH 必须正好有两位数
      • @irreputable 我知道这会发生,不认为这很重要,但会编辑
      • utf-8 现在但我担心我以后可能不得不处理多字节。谢谢。
      • 最新的编辑应该是多字节友好的;仅使用 UTF8 进行测试。
      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2013-04-01
      • 2018-01-27
      • 1970-01-01
      • 2011-03-26
      相关资源
      最近更新 更多