+ /**\r
+ * Checks if the {@link CharSequence} is a prefix of a valid number, and returns the NumberType it is a prefix of. If a String is a\r
+ * prefix of multiple different {@link NumberType}s, the one with the smallest radix is returned. If no valid {@link NumberType} is\r
+ * found, {@link NumberType#NONE} is returned.\r
+ * \r
+ * @param cs The potential prefix\r
+ * @return The type the {@link CharSequence} is a prefix of\r
+ */\r
+ public static NumberType prefixOfType(CharSequence cs)\r
+ {\r
+ if (isPrefix(cs, numberBin))\r
+ return NumberType.BINARY;\r
+ if (isPrefix(cs, numberOct))\r
+ return NumberType.OCTAL;\r
+ if (isPrefix(cs, numberDec))\r
+ return NumberType.DECIMAL;\r
+ if (isPrefix(cs, numberHex))\r
+ return NumberType.HEXADECIMAL;\r
+ if (isPrefix(cs, numberFloat))\r
+ return NumberType.FLOATINGPOINT;\r
+ return NumberType.NONE;\r
+ }\r
+\r
+ private static CharSequence extractSignAndDigits(NumberType type, CharSequence cs)\r
+ {\r
+ return type.numberPattern.matcher(cs).replaceAll("$1$2").replaceAll("_", "");\r
+ }\r
+\r
+ /**\r
+ * Computes the {@link BigInteger} value of a {@link CharSequence}, by determining the {@link NumberType} and parsing the number with\r
+ * the determined radix.\r
+ * \r
+ * @throws NumberFormatException if the {@link CharSequence} does not match any of the expected number patterns\r
+ */\r
+ public static BigInteger valueOf(CharSequence cs)\r
+ {\r
+ NumberType type = getType(cs);\r
+ if (NumberType.NONE.equals(type))\r
+ throw new NumberFormatException();\r
+ return new BigInteger(extractSignAndDigits(type, cs).toString(), type.radix);\r
+ }\r
+\r
+ /**\r
+ * Formats a {@link BigInteger} in accordance with the pattern associated with the supplied {@link NumberType}\r
+ */\r
+ public static String toString(BigInteger number, NumberType type)\r
+ {\r
+ String pattern;\r
+ switch (type)\r
+ {\r
+ case BINARY:\r
+ pattern = preferredBinPat;\r
+ break;\r
+ case OCTAL:\r
+ pattern = preferredOctPat;\r
+ break;\r
+ default:\r
+ case DECIMAL:\r
+ pattern = preferredDecPat;\r
+ break;\r
+ case HEXADECIMAL:\r
+ pattern = preferredHexPat;\r
+ }\r
+ if (number.signum() < 0)\r
+ return String.format(pattern, "-", number.abs().toString(type.radix));\r
+ return String.format(pattern, "", number.toString(type.radix));\r
+ }\r
+\r