java.lang.Character.digit()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(634)

本文整理了Java中java.lang.Character.digit()方法的一些代码示例,展示了Character.digit()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Character.digit()方法的具体详情如下:
包路径:java.lang.Character
类名称:Character
方法名:digit

Character.digit介绍

[英]Convenience method to determine the value of the specified character c in the supplied radix. The value of radix must be between MIN_RADIX and MAX_RADIX.
[中]确定所提供基数中指定字符c值的简便方法。基数的值必须介于最小基数和最大基数之间。

代码示例

代码示例来源:origin: stackoverflow.com

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;
}

代码示例来源:origin: libgdx/libgdx

public static int hexToBytes (String hex, byte[] data) {
    int len = hex.length();
    for (int i = 0; i < len; i += 2) {
      data[i >> 1] = (byte)((Character.digit(hex.charAt(i), 16) << 4) | Character.digit(hex.charAt(i + 1), 16));
    }
    return len / 2;
  }
}

代码示例来源:origin: libgdx/libgdx

public static int hexToBytes (String hex, byte[] data) {
    int len = hex.length();
    for (int i = 0; i < len; i += 2) {
      data[i >> 1] = (byte)((Character.digit(hex.charAt(i), 16) << 4) | Character.digit(hex.charAt(i + 1), 16));
    }
    return len / 2;
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>
 * Converts a hexadecimal digit into an int using the default (Lsb0) bit ordering.
 * </p>
 * <p>
 * '1' is converted to 1
 * </p>
 *
 * @param hexDigit the hexadecimal digit to convert
 * @return an int equals to {@code hexDigit}
 * @throws IllegalArgumentException if {@code hexDigit} is not a hexadecimal digit
 */
public static int hexDigitToInt(final char hexDigit) {
  final int digit = Character.digit(hexDigit, 16);
  if (digit < 0) {
    throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit");
  }
  return digit;
}

代码示例来源:origin: prestodb/presto

private static String convertToAsciiNumber(String convId) {
  StringBuilder buf = new StringBuilder(convId);
  for (int i = 0; i < buf.length(); i++) {
    char ch = buf.charAt(i);
    int digit = Character.digit(ch, 10);
    if (digit >= 0) {
      buf.setCharAt(i, (char) ('0' + digit));
    }
  }
  return buf.toString();
}

代码示例来源:origin: stackoverflow.com

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;
}

代码示例来源:origin: SonarSource/sonarqube

public ByteArray(String hexString) {
 int len = hexString.length();
 this.bytes = new byte[len / 2];
 for (int i = 0; i < len; i += 2) {
  bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
 }
}

代码示例来源:origin: google/ExoPlayer

/**
 * Returns a byte array containing values parsed from the hex string provided.
 *
 * @param hexString The hex string to convert to bytes.
 * @return A byte array containing values parsed from the hex string provided.
 */
public static byte[] getBytesFromHexString(String hexString) {
 byte[] data = new byte[hexString.length() / 2];
 for (int i = 0; i < data.length; i++) {
  int stringOffset = i * 2;
  data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4)
    + Character.digit(hexString.charAt(stringOffset + 1), 16));
 }
 return data;
}

代码示例来源:origin: joda-time/joda-time

private static String convertToAsciiNumber(String convId) {
  StringBuilder buf = new StringBuilder(convId);
  for (int i = 0; i < buf.length(); i++) {
    char ch = buf.charAt(i);
    int digit = Character.digit(ch, 10);
    if (digit >= 0) {
      buf.setCharAt(i, (char) ('0' + digit));
    }
  }
  return buf.toString();
}

代码示例来源:origin: lets-blade/blade

public static long parseUnsignedLong(String s, int radix) {
  int  len    = s.length();
  long first  = Long.parseLong(s.substring(0, len - 1), radix);
  int  second = Character.digit(s.charAt(len - 1), radix);
  return first * radix + second;
}

代码示例来源:origin: lets-blade/blade

public static long parseUnsignedLong(String s, int radix) {
  int  len    = s.length();
  long first  = Long.parseLong(s.substring(0, len - 1), radix);
  int  second = Character.digit(s.charAt(len - 1), radix);
  return first * radix + second;
}

代码示例来源:origin: JodaOrg/joda-time

private static String convertToAsciiNumber(String convId) {
  StringBuilder buf = new StringBuilder(convId);
  for (int i = 0; i < buf.length(); i++) {
    char ch = buf.charAt(i);
    int digit = Character.digit(ch, 10);
    if (digit >= 0) {
      buf.setCharAt(i, (char) ('0' + digit));
    }
  }
  return buf.toString();
}

代码示例来源:origin: stackoverflow.com

public static boolean isInteger(String s) {
  return isInteger(s,10);
}

public static boolean isInteger(String s, int radix) {
  if(s.isEmpty()) return false;
  for(int i = 0; i < s.length(); i++) {
    if(i == 0 && s.charAt(i) == '-') {
      if(s.length() == 1) return false;
      else continue;
    }
    if(Character.digit(s.charAt(i),radix) < 0) return false;
  }
  return true;
}

代码示例来源:origin: spring-projects/spring-framework

private static void verifyUriComponent(@Nullable String source, Type type) {
  if (source == null) {
    return;
  }
  int length = source.length();
  for (int i = 0; i < length; i++) {
    char ch = source.charAt(i);
    if (ch == '%') {
      if ((i + 2) < length) {
        char hex1 = source.charAt(i + 1);
        char hex2 = source.charAt(i + 2);
        int u = Character.digit(hex1, 16);
        int l = Character.digit(hex2, 16);
        if (u == -1 || l == -1) {
          throw new IllegalArgumentException("Invalid encoded sequence \"" +
              source.substring(i) + "\"");
        }
        i += 2;
      }
      else {
        throw new IllegalArgumentException("Invalid encoded sequence \"" +
            source.substring(i) + "\"");
      }
    }
    else if (!type.isAllowed(ch)) {
      throw new IllegalArgumentException("Invalid character '" + ch + "' for " +
          type.name() + " in \"" + source + "\"");
    }
  }
}

代码示例来源:origin: google/guava

long value = 0;
for (int pos = 0; pos < string.length(); pos++) {
 int digit = Character.digit(string.charAt(pos), radix);
 if (digit == -1) {
  throw new NumberFormatException(string);

代码示例来源:origin: stackoverflow.com

public static DecimalBigInt valueOf(String text, int radix) {
  DecimalBigInt bigRadix = new DecimalBigInt(radix);
  DecimalBigInt value = new DecimalBigInt(); // 0
  for(char digit : text.toCharArray()) {
    DecimalBigInt bigDigit =
      new DecimalBigInt(Character.digit(digit, radix));
    value = value.times(bigRadix).plus(bigDigit);
  }
  return value;
}

代码示例来源:origin: prestodb/presto

@VisibleForTesting
static int parseRgb(Slice color)
{
  if (color.length() != 4 || color.getByte(0) != '#') {
    return -1;
  }
  int red = Character.digit((char) color.getByte(1), 16);
  int green = Character.digit((char) color.getByte(2), 16);
  int blue = Character.digit((char) color.getByte(3), 16);
  if (red == -1 || green == -1 || blue == -1) {
    return -1;
  }
  // replicate the nibbles to turn a color of the form #rgb => #rrggbb (css semantics)
  red = (red << 4) | red;
  green = (green << 4) | green;
  blue = (blue << 4) | blue;
  return (int) rgb(red, green, blue);
}

代码示例来源:origin: google/guava

} else if (Character.digit(c, 16) == -1) {
 return null; // Everything else must be a decimal or hex digit.

代码示例来源:origin: netty/netty

private int parseInt(int start, int end, int radix, boolean negative) {
  int max = Integer.MIN_VALUE / radix;
  int result = 0;
  int currOffset = start;
  while (currOffset < end) {
    int digit = Character.digit((char) (value[currOffset++ + offset] & 0xFF), radix);
    if (digit == -1) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    if (max > result) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    int next = result * radix - digit;
    if (next > result) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    result = next;
  }
  if (!negative) {
    result = -result;
    if (result < 0) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
  }
  return result;
}

代码示例来源:origin: netty/netty

private long parseLong(int start, int end, int radix, boolean negative) {
  long max = Long.MIN_VALUE / radix;
  long result = 0;
  int currOffset = start;
  while (currOffset < end) {
    int digit = Character.digit((char) (value[currOffset++ + offset] & 0xFF), radix);
    if (digit == -1) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    if (max > result) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    long next = result * radix - digit;
    if (next > result) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
    result = next;
  }
  if (!negative) {
    result = -result;
    if (result < 0) {
      throw new NumberFormatException(subSequence(start, end, false).toString());
    }
  }
  return result;
}

相关文章

Character类方法