Example usage for org.json JSONTokener dehexchar

List of usage examples for org.json JSONTokener dehexchar

Introduction

In this page you can find the example usage for org.json JSONTokener dehexchar.

Prototype

public static int dehexchar(char c) 

Source Link

Document

Get the hex value of a character (base16).

Usage

From source file:org.official.json.Cookie.java

/**
 * Convert <code>%</code><i>hh</i> sequences to single characters, and
 * convert plus to space./*  w  w  w.j  a  v a2 s . c o  m*/
 * @param string A string that may contain
 *      <code>+</code>&nbsp;<small>(plus)</small> and
 *      <code>%</code><i>hh</i> sequences.
 * @return The unescaped string.
 */
public static String unescape(String string) {
    int length = string.length();
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; ++i) {
        char c = string.charAt(i);
        if (c == '+') {
            c = ' ';
        } else if (c == '%' && i + 2 < length) {
            int d = JSONTokener.dehexchar(string.charAt(i + 1));
            int e = JSONTokener.dehexchar(string.charAt(i + 2));
            if (d >= 0 && e >= 0) {
                c = (char) (d * 16 + e);
                i += 2;
            }
        }
        sb.append(c);
    }
    return sb.toString();
}