List of usage examples for java.lang Character digit
public static int digit(int codePoint, int radix)
From source file:StringUtils.java
public static byte[] hex2byte(String sinput) { int length = sinput.length(); if ((length & 0x01) != 0) { throw new IllegalArgumentException("odd number of characters."); }/*from w w w. ja v a 2 s . c om*/ byte[] out = new byte[length >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < length; i++) { int f = Character.digit(sinput.charAt(j++), 16) << 4; f = f | Character.digit(sinput.charAt(j++), 16); out[i] = (byte) (f & 0xFF); } return out; }
From source file:Main.java
public static byte[] hexStringToByteArray(String s) { if (s.length() % 2 != 0) { StringBuilder stringBuilder = new StringBuilder(s); stringBuilder.insert(s.length() - 1, "0"); s = stringBuilder.toString();//w w w .j a v a2 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:br.msf.commons.text.HexUtils.java
/** * Converts an hex number to its byte notation. * * @param hexString The String representing the hex number. * @return The set of bytes that represents the given hex number. *///from www .j a v a 2 s . com public static byte[] toBytes(final String hexString) { final String unformatted = unformat(hexString); ArgumentUtils.rejectIfDontMatches(unformatted, HEX_PATTERN); int length = unformatted.length() / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { int high = Character.digit(unformatted.charAt(i * 2), 16); int low = Character.digit(unformatted.charAt(i * 2 + 1), 16); int value = (high << 4) | low; if (value > 127) { value -= 256; } raw[i] = (byte) value; } return raw; }
From source file:org.mule.util.StringUtils.java
/** * Convert a hexadecimal string into its byte representation. * //from w w w . ja v a 2s . co m * @param hex The hexadecimal string. * @return The converted bytes or <code>null</code> if the hex String is null. */ public static byte[] hexStringToByteArray(String hex) { if (hex == null) { return null; } int stringLength = hex.length(); if (stringLength % 2 != 0) { throw new IllegalArgumentException("Hex String must have even number of characters!"); } byte[] result = new byte[stringLength / 2]; int j = 0; for (int i = 0; i < result.length; i++) { char hi = Character.toLowerCase(hex.charAt(j++)); char lo = Character.toLowerCase(hex.charAt(j++)); result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16)); } return result; }
From source file:StringUtils.java
private static int hexToDigit(final char ch, final int index) { final int digit = Character.digit(ch, 16); if (digit == -1) { throw new IllegalArgumentException("Illegal HexaDecimal character '" + ch + "' at index " + index); }/*from ww w .j ava 2 s .co m*/ return digit; }
From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java
/** * Converts a string to an array of bytes. * //from ww w. j av a 2 s. c o m * @param s * the string to convert. * @return the converted array of bytes. */ public static byte[] hexStringToByteArray(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)); } return data; }
From source file:bin.spider.frame.uri.LaxURLCodec.java
/** * Decodes an array of URL safe 7-bit characters into an array of * original bytes. Escaped characters are converted back to their * original representation./* www. j ava 2s . co m*/ * * Differs from URLCodec.decodeUrl() in that it throws no * exceptions; bad or incomplete escape sequences are ignored * and passed into result undecoded. This matches the behavior * of browsers, which will use inconsistently-encoded URIs * in HTTP request-lines. * * @param bytes array of URL safe characters * @return array of original bytes */ public static final byte[] decodeUrlLoose(byte[] bytes) { if (bytes == null) { return null; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { int b = bytes[i]; if (b == '+') { buffer.write(' '); continue; } if (b == '%') { if (i + 2 < bytes.length) { int u = Character.digit((char) bytes[i + 1], 16); int l = Character.digit((char) bytes[i + 2], 16); if (u > -1 && l > -1) { // good encoding int c = ((u << 4) + l); buffer.write((char) c); i += 2; continue; } // else: bad encoding digits, leave '%' in place } // else: insufficient encoding digits, leave '%' in place } buffer.write(b); } return buffer.toByteArray(); }
From source file:Utils.java
public static byte[] fromHex(final char[] hex) { final int length = hex.length / 2; final byte[] raw = new byte[length]; for (int i = 0; i < length; i++) { final int high = Character.digit(hex[i * 2], 16); final int low = Character.digit(hex[i * 2 + 1], 16); int value = (high << 4) | low; if (value > 127) { value -= 256;/* w w w . j a v a 2 s . com*/ } raw[i] = (byte) value; } return raw; }
From source file:Main.java
/** * Generates a formatted text string given a source string containing * "argument markers" of the form "{argNum}" where each argNum must be in * the range 0..9. The result is generated by inserting the toString of each * argument into the position indicated in the string. * <p>//from w w w . j av a2s .c o m * To insert the "{" character into the output, use a single backslash * character to escape it (i.e. "\{"). The "}" character does not need to be * escaped. * * @param format * String the format to use when printing. * @param args * Object[] the arguments to use. * @return String the formatted message. */ public static String format(String format, Object[] args) { StringBuilder answer = new StringBuilder(format.length() + (args.length * 20)); String[] argStrings = new String[args.length]; for (int i = 0; i < args.length; ++i) { if (args[i] == null) argStrings[i] = "<null>"; else argStrings[i] = args[i].toString(); } int lastI = 0; for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) { if (i != 0 && format.charAt(i - 1) == '\\') { // It's escaped, just print and loop. if (i != 1) answer.append(format.substring(lastI, i - 1)); answer.append('{'); lastI = i + 1; } else { // It's a format character. if (i > format.length() - 3) { // Bad format, just print and loop. answer.append(format.substring(lastI, format.length())); lastI = format.length(); } else { int argnum = (byte) Character.digit(format.charAt(i + 1), 10); if (argnum < 0 || format.charAt(i + 2) != '}') { // Bad format, just print and loop. answer.append(format.substring(lastI, i + 1)); lastI = i + 1; } else { // Got a good one! answer.append(format.substring(lastI, i)); if (argnum >= argStrings.length) answer.append("<missing argument>"); else answer.append(argStrings[argnum]); lastI = i + 3; } } } } if (lastI < format.length()) answer.append(format.substring(lastI, format.length())); return answer.toString(); }
From source file:org.talend.dataquality.semantic.validator.impl.SedolValidator.java
public int getSedolCheckDigit(String str) { String strUpper = str.toUpperCase(); int total = 0; for (int i = 0; i < 6; i++) { char s = strUpper.charAt(i); total += Character.digit(s, 36) * mult[i]; }/* ww w . j a va 2 s . c om*/ return (10 - (total % 10)) % 10; }