List of usage examples for java.lang Character digit
public static int digit(int codePoint, int radix)
From source file:Main.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/*from w ww . j av a2 s. c o m*/ */ 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:Main.java
public static String decodeQ(String str, String charsetName) throws IOException { byte[] bs = str.getBytes(); int len = bs.length; int length = 0; for (int i = 0; i < len; i++) { byte b = bs[i]; if (b == '=') { i++;// w w w . j a v a2 s . c o m if (i == len) break; b = bs[i]; if (b == '\r' || b == '\n') { b = bs[++i]; if (b != '\n') { i--; } continue; } int result = -Character.digit(b, 16); result *= 16; result -= Character.digit(bs[++i], 16); bs[length++] = (byte) -result; } else { bs[length++] = b; } } return new String(bs, 0, length, charsetName); }
From source file:com.easemob.dataexport.utils.StringUtils.java
public static byte[] hexToBytes(String hexString) { char[] hex = hexString.toCharArray(); int length = hex.length / 2; byte[] rawData = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); int value = (high << 4) | low; if (value > 127) { value -= 256;/* www . j a va 2 s . c om*/ } rawData[i] = (byte) value; } return rawData; }
From source file:Main.java
public static byte[] fromHex(String s) { if (s != null) { try {//from www. j av a 2 s . c o m StringBuilder sb = new StringBuilder(s.length()); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (!Character.isWhitespace(ch)) { sb.append(ch); } } s = sb.toString(); int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { int hi = (Character.digit(s.charAt(i), 16) << 4); int low = Character.digit(s.charAt(i + 1), 16); if (hi >= 256 || low < 0 || low >= 16) { return null; } data[i / 2] = (byte) (hi | low); } return data; } catch (Exception ignored) { } } return null; }
From source file:org.archone.ad.ldap.PasswordUtil.java
public static byte[] hexToBytes(char[] hex) { int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(hex[i * 2], 16); int low = Character.digit(hex[i * 2 + 1], 16); int value = (high << 4) | low; if (value > 127) { value -= 256;/*www . ja va 2s . com*/ } raw[i] = (byte) value; } return raw; }
From source file:Main.java
/** Convert a hexidecimal string generated by toHexString() back into a byte array.// w ww . j ava2 s. c o m @param s String to convert @param offset starting character (zero based) to convert. @param length number of characters to convert. @return the converted byte array. Returns null if the length is not a multiple of 2. */ public static byte[] fromHexString(String s, int offset, int length) { if ((length % 2) != 0) return null; byte[] byteArray = new byte[length / 2]; int j = 0; int end = offset + length; for (int i = offset; i < end; i += 2) { int high_nibble = Character.digit(s.charAt(i), 16); int low_nibble = Character.digit(s.charAt(i + 1), 16); if (high_nibble == -1 || low_nibble == -1) { // illegal format return null; } byteArray[j++] = (byte) (((high_nibble << 4) & 0xf0) | (low_nibble & 0x0f)); } return byteArray; }
From source file:bobs.mcapisignature.CertUtils.java
public static byte[] hexStringToByteArray(String s) { s = s.replaceAll(" ", ""); s = s.replaceAll("-", ""); s = s.replaceAll(":", ""); s = s.toUpperCase();/*from w w w . j a va 2 s . c o m*/ int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; }
From source file:com.os.util.PasswordDecoderEncoder.java
public static byte[] convertHexToBytes(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); }/*from w ww . jav a 2s .c o m*/ return data; }
From source file:org.apache.hadoop.io.crypto.bee.Hex.java
public static byte[] decode(CharSequence s) { int nChars = s.length(); if (nChars % 2 != 0) { throw new IllegalArgumentException("Hex-encoded string must have an even number of characters"); }/*from w w w.j ava2 s . c o m*/ byte[] result = new byte[nChars / 2]; for (int i = 0; i < nChars; i += 2) { int msb = Character.digit(s.charAt(i), 16); int lsb = Character.digit(s.charAt(i + 1), 16); if (msb < 0 || lsb < 0) { throw new IllegalArgumentException("Non-hex character in input: " + s); } result[i / 2] = (byte) ((msb << 4) | lsb); } return result; }
From source file:Main.java
/** * Converts an ASCII representation of a Bitmap field * into a Java BitSet/*from w w w .j a v a 2s .co m*/ * @param b - hex representation * @param offset - starting offset * @param maxBits - max number of bits (supports 8, 16, 24, 32, 48, 52, 64,.. 128 or 192) * @return java BitSet object */ public static BitSet hex2BitSet(byte[] b, int offset, int maxBits) { int len = maxBits > 64 ? ((Character.digit((char) b[offset], 16) & 0x08) == 8 ? 128 : 64) : maxBits; BitSet bmap = new BitSet(len); for (int i = 0; i < len; i++) { int digit = Character.digit((char) b[offset + (i >> 2)], 16); if ((digit & (0x08 >> (i % 4))) > 0) { bmap.set(i + 1); if (i == 65 && maxBits > 128) len = 192; } } return bmap; }