Convert an inline encoded hex string rendition of an ASN.1
object back into its corresponding ASN.1 object.

@param str the hex encoded object
@param off the index at which the encoding starts
@return the decoded object
549397
protected DERObject convertHexEncoded(
        String  str,
        int     off)
        throws IOException
    {
        str = str.toLowerCase();
        byte[]    data = new byte[str.length() / 2];
        for (int index = 0; index != data.length; index++)
        {
            char left = str.charAt((index * 2) + off);
            char right = str.charAt((index * 2) + off + 1);

            if (left < 'a')
            {
                data[index] = (byte)((left - '0') << 4);
            }
            else
            {
                data[index] = (byte)((left - 'a' + 10) << 4);
            }
            if (right < 'a')
            {
                data[index] |= (byte)(right - '0');
            }
            else
            {
                data[index] |= (byte)(right - 'a' + 10);
            }
        }

        ASN1InputStream aIn = new ASN1InputStream(
                                            new ByteArrayInputStream(data));

        return aIn.readObject();
    }
958349
private static void convertHexToUnicode(String str) {
        try {
            display("Hex to Unicode: " + Utils.toString(Utils.base64ToBinary(Utils.base64FromHex(str))));
        } catch (Exception e) {
        }
    }
603224
public static String substring(String str, int off, int len) {
	return str.substring(off, off + len);
    }
307191
public static char[] encodeHex(String str, Charset charset) {
		return encodeHex(StrUtil.bytes(str, charset), true);
	}
1230139
public static <T> T deserialize(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            bis = new ByteArrayInputStream(
                    decoder.decode(str));
            ois = new ObjectInputStream(bis);
            return (T) ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        } finally {
            CommonUtils.closeIOStream(bis, null);
            CommonUtils.closeIOStream(ois, null);
        }
    }
514744
public static String fwsTohws( String str )
    {
        char[] chars = str.toCharArray();
        for ( int j = 0; j < chars.length; j++ ) {
            if ( chars[j] == '\u3000' ) {
                chars[j] = '\u0020';
            } else if ( chars[j] > '\uFF00' 
                && chars[j] < '\uFF5F' ) {
                chars[j] = (char)(chars[j] - 65248);
            }       
        }
        
        return new String(chars);
    }
489600
private static String decode(final String str, final int firstPercent) {
        StringBuilder builder = new StringBuilder();
        builder.append(str.substring(0, firstPercent));

        appendDecoded(builder, str, firstPercent);

        for (int i = firstPercent + 3; i < str.length(); ++i) {
            if (str.charAt(i) == '%') {
                appendDecoded(builder, str, i);
                i += 2;
            } else {
                builder.append(str.charAt(i));
            }
        }

        return builder.toString();
    }
748181
public static byte[] decodeHex(String hexStr) {
		if (StrUtil.isEmpty(hexStr)) {
			return null;
		}
		return decodeHex(hexStr.toCharArray());
	}
294814
public static byte[] fromHexString(String hexStr) {
    if (!hexStr.matches("^[0-9A-Fa-f]*$"))
      throw new IllegalArgumentException("Invalid hexadecimal string");
    if (hexStr.isEmpty()) return new byte[0];

    int complementary = hexStr.length() % 2;
    if (complementary != 0) hexStr += "0";
    return rjust(new BigInteger(hexStr, 16).toByteArray(), hexStr.length() / 2);
  }
208897
private static String decode(final String str) {
        if (str == null) {
            return null;
        }

        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == '%') {
                return decode(str, i);
            }
        }

        return str;
    }
137083
def fromhexstring(cls, hexstring):
        
        bitstring = format(int(hexstring, 16), "0" + str(len(hexstring)/4) + "b")
        return cls.fromstring(bitstring)
1372999
protected static String getAsciiStringFrom6BitStr(String str) {

		StringBuilder txt = new StringBuilder();
		for (int i = 0; i < str.length(); i = i + 6) {
			byte _byte = (byte) Integer.parseInt(str.substring(i, i + 6), 2);
			_byte = convert6BitCharToStandardAscii(_byte);
			char convChar = (char) _byte;

			if (convChar == '@') {
				break;
			}
			txt.append((char) _byte);
		}

		return txt.toString().trim();
	}
948720
public static String toHexString(byte[] src, int offset, int length) {
        return toHexString(new StringBuilder(length << 1), src, offset, length).toString();
    }
262420
static byte[] fromHexString(String str) {
    if (str.length() % 2 != 0) {
      throw new InvalidOperationException("Hex-string has odd length!");
    }
    byte[] data = new byte[str.length() / 2];
    int spos = 0;
    for (int dpos = 0; dpos < data.length; dpos++) {
      int d1 = Character.digit(str.charAt(spos++), 16);
      int d2 = Character.digit(str.charAt(spos++), 16);
      if (d1 < 0 || d2 < 0) {
        throw new InvalidOperationException("Mal-formed hex-string!");
      }
      data[dpos] = (byte) ((d1 << 4) | d2);
    }
    return data;
  }
491795
static byte[] decodeBase32(String str) {
		// each base-32 character encodes 5 bits
		int numBytes = ((str.length() * 5) + 7) / 8;
		byte[] result = new byte[numBytes];
		int resultIndex = 0;
		int which = 0;
		int working = 0;
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			int val;
			if (ch >= 'a' && ch <= 'z') {
				val = ch - 'a';
			} else if (ch >= 'A' && ch <= 'Z') {
				val = ch - 'A';
			} else if (ch >= '2' && ch <= '7') {
				val = 26 + (ch - '2');
			} else if (ch == '=') {
				// special case
				which = 0;
				break;
			} else {
				throw new IllegalArgumentException("Invalid base-32 character: " + ch);
			}
			/*
			 * There are probably better ways to do this but this seemed the most straightforward.
			 */
			switch (which) {
				case 0:
					// all 5 bits is top 5 bits
					working = (val & 0x1F) << 3;
					which = 1;
					break;
				case 1:
					// top 3 bits is lower 3 bits
					working |= (val & 0x1C) >> 2;
					result[resultIndex++] = (byte) working;
					// lower 2 bits is upper 2 bits
					working = (val & 0x03) << 6;
					which = 2;
					break;
				case 2:
					// all 5 bits is mid 5 bits
					working |= (val & 0x1F) << 1;
					which = 3;
					break;
				case 3:
					// top 1 bit is lowest 1 bit
					working |= (val & 0x10) >> 4;
					result[resultIndex++] = (byte) working;
					// lower 4 bits is top 4 bits
					working = (val & 0x0F) << 4;
					which = 4;
					break;
				case 4:
					// top 4 bits is lowest 4 bits
					working |= (val & 0x1E) >> 1;
					result[resultIndex++] = (byte) working;
					// lower 1 bit is top 1 bit
					working = (val & 0x01) << 7;
					which = 5;
					break;
				case 5:
					// all 5 bits is mid 5 bits
					working |= (val & 0x1F) << 2;
					which = 6;
					break;
				case 6:
					// top 2 bits is lowest 2 bits
					working |= (val & 0x18) >> 3;
					result[resultIndex++] = (byte) working;
					// lower 3 bits of byte 6 is top 3 bits
					working = (val & 0x07) << 5;
					which = 7;
					break;
				case 7:
					// all 5 bits is lower 5 bits
					working |= (val & 0x1F);
					result[resultIndex++] = (byte) working;
					which = 0;
					break;
			}
		}
		if (which != 0) {
			result[resultIndex++] = (byte) working;
		}
		if (resultIndex != result.length) {
			result = Arrays.copyOf(result, resultIndex);
		}
		return result;
	}
1283168
protected String decodePercent(String str) {
        String decoded = null;
        try {
            decoded = URLDecoder.decode(str, "UTF8");
        } catch (UnsupportedEncodingException ignored) {
        }
        return decoded;
    }
503847
public static String encodeToSafeAsciiFileNameString(String str, Charset strCharset) {
        if (str == null) {
            return str;
        }
        if (!SAFE_ASCII_PATTERN.matcher(str).matches()) {
            return calcSHA1Digest(str, strCharset);
        }
        // encode again since if str is SHA1 digest
        // it can easily conflict with the other encoded string
        if (SHA1_DIGEST_PATTERN.matcher(str).matches()) {
            return calcSHA1Digest(str, strCharset);
        }
        return str;
    }