List of usage examples for java.lang Character digit
public static int digit(int codePoint, int radix)
From source file:com.gistlabs.mechanize.util.apache.URLEncodedUtils.java
/** * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true. * //ww w. j a v a2 s . c o m * @param content the portion to decode * @param charset the charset to use * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is. * @return */ private static String urldecode(final String content, final Charset charset, final boolean plusAsBlank) { if (content == null) return null; ByteBuffer bb = ByteBuffer.allocate(content.length()); CharBuffer cb = CharBuffer.wrap(content); while (cb.hasRemaining()) { char c = cb.get(); if (c == '%' && cb.remaining() >= 2) { char uc = cb.get(); char lc = cb.get(); int u = Character.digit(uc, 16); int l = Character.digit(lc, 16); if (u != -1 && l != -1) bb.put((byte) ((u << 4) + l)); else { bb.put((byte) '%'); bb.put((byte) uc); bb.put((byte) lc); } } else if (plusAsBlank && c == '+') bb.put((byte) ' '); else bb.put((byte) c); } bb.flip(); return charset.decode(bb).toString(); }
From source file:com.evolveum.midpoint.util.MiscUtil.java
public static byte[] hexToBinary(String hex) { int l = hex.length(); byte[] bytes = new byte[l / 2]; for (int i = 0; i < l; i += 2) { bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); }//from www .java 2 s . c o m return bytes; }
From source file:Animator.java
/** * Substitute an integer some number of times in a string, subject to * parameter strings embedded in the string. Parameter strings: %N - * substitute the integer as is, with no padding. % <digit>, for example %5 - * substitute the integer left-padded with zeros to <digits>digits wide. %% - * substitute a '%' here.//from ww w . ja v a 2s . co m * * @param inStr * the String to substitute within * @param theInt * the int to substitute. */ String doSubst(String inStr, int theInt) { String padStr = "0000000000"; int length = inStr.length(); StringBuffer result = new StringBuffer(length); for (int i = 0; i < length;) { char ch = inStr.charAt(i); if (ch == '%') { i++; if (i == length) { result.append(ch); } else { ch = inStr.charAt(i); if (ch == 'N') { // just stick in the number, unmolested result.append(theInt + ""); i++; } else { int pad; if ((pad = Character.digit(ch, 10)) != -1) { // we've got a width value String numStr = theInt + ""; String scr = padStr + numStr; result.append(scr.substring(scr.length() - pad)); i++; } else { result.append(ch); i++; } } } } else { result.append(ch); i++; } } return result.toString(); }
From source file:com.example.android.lightcontrol.MainActivity.java
public static byte[] hexStringToByteArray(String send1) { int len = send1.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(send1.charAt(i), 16) << 4) + Character.digit(send1.charAt(i + 1), 16)); }/*w w w . j a v a2 s .c om*/ return data; }
From source file:com.pandoroid.pandora.PandoraRadio.java
/** * Description: Self explanatory function that converts from a hex string * to a plain string. One complicated portion of this to mention is that * String types can do something rather odd when conversions are made * from byte arrays to Strings and back again. They don't like to work * out perfectly./*from ww w . j a va2 s.co m*/ */ private byte[] fromHex(String hex_text) { int hex_len = hex_text.length(); byte[] raw = new byte[hex_len / 2]; for (int i = 0; i < hex_len; i += 2) { raw[i / 2] = (byte) ((Character.digit(hex_text.charAt(i), 16) * 16) + Character.digit(hex_text.charAt(i + 1), 16)); } return raw; }
From source file:com.mcxiaoke.next.http.util.URLUtils.java
/** * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true. * * @param content the portion to decode * @param charset the charset to use * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is. * @return encoded string// w ww .j a v a2s . c o m */ private static String urlDecode(final String content, final Charset charset, final boolean plusAsBlank) { if (content == null) { return null; } final ByteBuffer bb = ByteBuffer.allocate(content.length()); final CharBuffer cb = CharBuffer.wrap(content); while (cb.hasRemaining()) { final char c = cb.get(); if (c == '%' && cb.remaining() >= 2) { final char uc = cb.get(); final char lc = cb.get(); final int u = Character.digit(uc, 16); final int l = Character.digit(lc, 16); if (u != -1 && l != -1) { bb.put((byte) ((u << 4) + l)); } else { bb.put((byte) '%'); bb.put((byte) uc); bb.put((byte) lc); } } else if (plusAsBlank && c == '+') { bb.put((byte) ' '); } else { bb.put((byte) c); } } bb.flip(); return charset.decode(bb).toString(); }
From source file:biz.bokhorst.xprivacy.Util.java
private static byte[] hex2bytes(String hex) { // Convert hex string to byte array int len = hex.length(); byte[] result = new byte[len / 2]; for (int i = 0; i < len; i += 2) result[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); return result; }
From source file:com.qut.middleware.spep.filter.SPEPFilter.java
/** * Transcodes %XX symbols per RFC 2369 to normalized character format - adapted from apache commons * /*from ww w. j a va 2s. co m*/ * @param buffer * The stringBuffer object containing the request data * @param offset * How far into the string buffer we should start processing from * @param length * Number of chars in the buffer * @throws ServletException */ private void decode(final StringBuffer buffer, final int offset, final int length) throws ServletException { int index = offset; int count = length; int dig1, dig2; while (count > 0) { final char ch = buffer.charAt(index); if (ch != '%') { count--; index++; continue; } if (count < 3) { throw new ServletException( Messages.getString("SPEPFilter.10") + buffer.substring(index, index + count)); //$NON-NLS-1$ } dig1 = Character.digit(buffer.charAt(index + 1), 16); dig2 = Character.digit(buffer.charAt(index + 2), 16); if (dig1 == -1 || dig2 == -1) { throw new ServletException( Messages.getString("SPEPFilter.11") + buffer.substring(index, index + count)); //$NON-NLS-1$ } char value = (char) (dig1 << 4 | dig2); buffer.setCharAt(index, value); buffer.delete(index + 1, index + 3); count -= 3; index++; } }
From source file:QCodec.java
/** * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted * back to their original representation. * //from w w w. j a v a 2 s . c om * <p> * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521. * </p> * * @param bytes * array of quoted-printable characters * @return array of original bytes * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful */ public static final byte[] decodeQuotedPrintable(byte[] bytes) throws Exception { if (bytes == null) { return null; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { int b = bytes[i]; if (b == ESCAPE_CHAR) { try { int u = Character.digit((char) bytes[++i], 16); int l = Character.digit((char) bytes[++i], 16); if (u == -1 || l == -1) { throw new RuntimeException("Invalid quoted-printable encoding"); } buffer.write((char) ((u << 4) + l)); } catch (ArrayIndexOutOfBoundsException e) { throw new RuntimeException("Invalid quoted-printable encoding"); } } else { buffer.write(b); } } return buffer.toByteArray(); }
From source file:org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.java
@SuppressWarnings("nls") public static String unescapeSQLString(String b) { Character enclosure = null;//from w w w .jav a 2 s . c o m // Some of the strings can be passed in as unicode. For example, the // delimiter can be passed in as \002 - So, we first check if the // string is a unicode number, else go back to the old behavior StringBuilder sb = new StringBuilder(b.length()); for (int i = 0; i < b.length(); i++) { char currentChar = b.charAt(i); if (enclosure == null) { if (currentChar == '\'' || b.charAt(i) == '\"') { enclosure = currentChar; } // ignore all other chars outside the enclosure continue; } if (enclosure.equals(currentChar)) { enclosure = null; continue; } if (currentChar == '\\' && (i + 6 < b.length()) && b.charAt(i + 1) == 'u') { int code = 0; int base = i + 2; for (int j = 0; j < 4; j++) { int digit = Character.digit(b.charAt(j + base), 16); code = (code << 4) + digit; } sb.append((char) code); i += 5; continue; } if (currentChar == '\\' && (i + 4 < b.length())) { char i1 = b.charAt(i + 1); char i2 = b.charAt(i + 2); char i3 = b.charAt(i + 3); if ((i1 >= '0' && i1 <= '1') && (i2 >= '0' && i2 <= '7') && (i3 >= '0' && i3 <= '7')) { byte bVal = (byte) ((i3 - '0') + ((i2 - '0') * 8) + ((i1 - '0') * 8 * 8)); byte[] bValArr = new byte[1]; bValArr[0] = bVal; String tmp = new String(bValArr); sb.append(tmp); i += 3; continue; } } if (currentChar == '\\' && (i + 2 < b.length())) { char n = b.charAt(i + 1); switch (n) { case '0': sb.append("\0"); break; case '\'': sb.append("'"); break; case '"': sb.append("\""); break; case 'b': sb.append("\b"); break; case 'n': sb.append("\n"); break; case 'r': sb.append("\r"); break; case 't': sb.append("\t"); break; case 'Z': sb.append("\u001A"); break; case '\\': sb.append("\\"); break; // The following 2 lines are exactly what MySQL does TODO: why do we do this? case '%': sb.append("\\%"); break; case '_': sb.append("\\_"); break; default: sb.append(n); } i++; } else { sb.append(currentChar); } } return sb.toString(); }