@Test public void testString2Int() { assertEquals(-1, TypeUtils.string2Int(null, TypeUtils.Radix.Decimal, -1)); assertEquals(-1, TypeUtils.string2Int("", TypeUtils.Radix.Decimal, -1)); assertEquals(-1, TypeUtils.string2Int("10asd", TypeUtils.Radix.Decimal, -1)); assertEquals(-1, TypeUtils.string2Int("asd230das", TypeUtils.Radix.Decimal, -1)); assertEquals(0, TypeUtils.string2Int("0", TypeUtils.Radix.Decimal, -1)); assertEquals(12, TypeUtils.string2Int("12", TypeUtils.Radix.Decimal, -1)); assertEquals(23434, TypeUtils.string2Int("23434", TypeUtils.Radix.Decimal, -1)); assertEquals(123, TypeUtils.string2Int("0123", TypeUtils.Radix.Decimal, -1)); assertEquals(12340, TypeUtils.string2Int("012340", TypeUtils.Radix.Decimal, -1)); assertEquals(-1, TypeUtils.string2Int(null, TypeUtils.Radix.Hexadecimal, -1)); assertEquals(-1, TypeUtils.string2Int("", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(-1, TypeUtils.string2Int("0x", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(0, TypeUtils.string2Int("0x0", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(1, TypeUtils.string2Int("0x1", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(1, TypeUtils.string2Int("0X1", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(15, TypeUtils.string2Int("0xf", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(15, TypeUtils.string2Int("0xF", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(16, TypeUtils.string2Int("0x10", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(255, TypeUtils.string2Int("0xff", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(0, TypeUtils.string2Int("0", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(1, TypeUtils.string2Int("1", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(1, TypeUtils.string2Int("1", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(15, TypeUtils.string2Int("f", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(15, TypeUtils.string2Int("F", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(16, TypeUtils.string2Int("10", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(255, TypeUtils.string2Int("ff", TypeUtils.Radix.Hexadecimal, -1)); assertEquals(-1, TypeUtils.string2Int(null, TypeUtils.Radix.Binary, -1)); assertEquals(-1, TypeUtils.string2Int("", TypeUtils.Radix.Binary, -1)); assertEquals(-1, TypeUtils.string2Int("0b", TypeUtils.Radix.Binary, -1)); assertEquals(0, TypeUtils.string2Int("0b0", TypeUtils.Radix.Binary, -1)); assertEquals(1, TypeUtils.string2Int("0b1", TypeUtils.Radix.Binary, -1)); assertEquals(1, TypeUtils.string2Int("0B1", TypeUtils.Radix.Binary, -1)); assertEquals(2, TypeUtils.string2Int("0b10", TypeUtils.Radix.Binary, -1)); assertEquals(3, TypeUtils.string2Int("0b11", TypeUtils.Radix.Binary, -1)); assertEquals(15, TypeUtils.string2Int("0b1111", TypeUtils.Radix.Binary, -1)); assertEquals(16, TypeUtils.string2Int("0b10000", TypeUtils.Radix.Binary, -1)); assertEquals(0, TypeUtils.string2Int("0", TypeUtils.Radix.Binary, -1)); assertEquals(1, TypeUtils.string2Int("1", TypeUtils.Radix.Binary, -1)); assertEquals(1, TypeUtils.string2Int("1", TypeUtils.Radix.Binary, -1)); assertEquals(2, TypeUtils.string2Int("10", TypeUtils.Radix.Binary, -1)); assertEquals(3, TypeUtils.string2Int("11", TypeUtils.Radix.Binary, -1)); assertEquals(15, TypeUtils.string2Int("1111", TypeUtils.Radix.Binary, -1)); assertEquals(16, TypeUtils.string2Int("10000", TypeUtils.Radix.Binary, -1)); assertEquals(-1, TypeUtils.string2Int(null, TypeUtils.Radix.Octonary, -1)); assertEquals(-1, TypeUtils.string2Int("", TypeUtils.Radix.Octonary, -1)); assertEquals(-1, TypeUtils.string2Int("08", TypeUtils.Radix.Octonary, -1)); assertEquals(0, TypeUtils.string2Int("080", TypeUtils.Radix.Octonary, -1)); assertEquals(1, TypeUtils.string2Int("081", TypeUtils.Radix.Octonary, -1)); assertEquals(8, TypeUtils.string2Int("0810", TypeUtils.Radix.Octonary, -1)); assertEquals(9, TypeUtils.string2Int("0811", TypeUtils.Radix.Octonary, -1)); assertEquals(15, TypeUtils.string2Int("0817", TypeUtils.Radix.Octonary, -1)); assertEquals(16, TypeUtils.string2Int("0820", TypeUtils.Radix.Octonary, -1)); assertEquals(0, TypeUtils.string2Int("0", TypeUtils.Radix.Octonary, -1)); assertEquals(1, TypeUtils.string2Int("1", TypeUtils.Radix.Octonary, -1)); assertEquals(6, TypeUtils.string2Int("6", TypeUtils.Radix.Octonary, -1)); assertEquals(8, TypeUtils.string2Int("10", TypeUtils.Radix.Octonary, -1)); assertEquals(9, TypeUtils.string2Int("11", TypeUtils.Radix.Octonary, -1)); assertEquals(15, TypeUtils.string2Int("17", TypeUtils.Radix.Octonary, -1)); assertEquals(16, TypeUtils.string2Int("20", TypeUtils.Radix.Octonary, -1)); }
public static int string2Int(String str, Radix radix, int defaultValue) { try { if (!StringUtils.isBlank(str)) { String check = str.toLowerCase(); if (check.startsWith("0b") || (check.startsWith("08") && radix == Radix.Octonary) || check.startsWith("0x")) { str = str.substring(2); } return Integer.parseInt(str, radix.getRadix()); } } catch (NumberFormatException ex) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, radix: %s, default: %d.", str, radix.name(), defaultValue)); } } return defaultValue; }
TypeUtils { public static int string2Int(String str, Radix radix, int defaultValue) { try { if (!StringUtils.isBlank(str)) { String check = str.toLowerCase(); if (check.startsWith("0b") || (check.startsWith("08") && radix == Radix.Octonary) || check.startsWith("0x")) { str = str.substring(2); } return Integer.parseInt(str, radix.getRadix()); } } catch (NumberFormatException ex) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, radix: %s, default: %d.", str, radix.name(), defaultValue)); } } return defaultValue; } }
TypeUtils { public static int string2Int(String str, Radix radix, int defaultValue) { try { if (!StringUtils.isBlank(str)) { String check = str.toLowerCase(); if (check.startsWith("0b") || (check.startsWith("08") && radix == Radix.Octonary) || check.startsWith("0x")) { str = str.substring(2); } return Integer.parseInt(str, radix.getRadix()); } } catch (NumberFormatException ex) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, radix: %s, default: %d.", str, radix.name(), defaultValue)); } } return defaultValue; } private  TypeUtils(); }
TypeUtils { public static int string2Int(String str, Radix radix, int defaultValue) { try { if (!StringUtils.isBlank(str)) { String check = str.toLowerCase(); if (check.startsWith("0b") || (check.startsWith("08") && radix == Radix.Octonary) || check.startsWith("0x")) { str = str.substring(2); } return Integer.parseInt(str, radix.getRadix()); } } catch (NumberFormatException ex) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, radix: %s, default: %d.", str, radix.name(), defaultValue)); } } return defaultValue; } private  TypeUtils(); static int byteArray2Int(byte[] bytes); static long byteArray2Long(byte[] bytes); static byte[] int2ByteArray(int i); static byte[] long2ByteArray(long l); static String byteArray2Ipv4(byte[] bytes); static String byteArray2Ipv6(byte[] bytes); static String byteArray2Ip(byte[] bytes); static List<String> csv2List(String line); static byte[] Ip2byteArrayV4(String ipv4); static byte[] Ip2byteArrayV6(String ipv6); static byte[] Ip2byteArray(String ip); static String byteArray2HexString(byte[] byteArray); static String intArray2HexString(int[] intArray); static String longArray2HexString(int[] longArray); static String byteArray2Base64(byte[] value); static byte[] base642ByteArray(String base64Str); static String byte2HexString(byte b); static byte[] hexString2ByteArray(String input); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, Radix radix, int defaultValue); static long string2Long(String str, Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long string2TimePeriod(String timePeriod, long defaultValue); static boolean equals(T t1, T t2); static boolean maybeUpdate(T tar, T src); }
TypeUtils { public static int string2Int(String str, Radix radix, int defaultValue) { try { if (!StringUtils.isBlank(str)) { String check = str.toLowerCase(); if (check.startsWith("0b") || (check.startsWith("08") && radix == Radix.Octonary) || check.startsWith("0x")) { str = str.substring(2); } return Integer.parseInt(str, radix.getRadix()); } } catch (NumberFormatException ex) { if (logger.isWarnEnabled()) { logger.warn(String.format("Parse %s to int fail, radix: %s, default: %d.", str, radix.name(), defaultValue)); } } return defaultValue; } private  TypeUtils(); static int byteArray2Int(byte[] bytes); static long byteArray2Long(byte[] bytes); static byte[] int2ByteArray(int i); static byte[] long2ByteArray(long l); static String byteArray2Ipv4(byte[] bytes); static String byteArray2Ipv6(byte[] bytes); static String byteArray2Ip(byte[] bytes); static List<String> csv2List(String line); static byte[] Ip2byteArrayV4(String ipv4); static byte[] Ip2byteArrayV6(String ipv6); static byte[] Ip2byteArray(String ip); static String byteArray2HexString(byte[] byteArray); static String intArray2HexString(int[] intArray); static String longArray2HexString(int[] longArray); static String byteArray2Base64(byte[] value); static byte[] base642ByteArray(String base64Str); static String byte2HexString(byte b); static byte[] hexString2ByteArray(String input); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, Radix radix, int defaultValue); static long string2Long(String str, Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long string2TimePeriod(String timePeriod, long defaultValue); static boolean equals(T t1, T t2); static boolean maybeUpdate(T tar, T src); static final long KB; static final long MB; static final long GB; static final long TB; static final long PB; static final long SEC; static final long MIN; static final long HOUR; static final long DAY; static final long WEEK; static final long MON; static final long QUAR; static final long YEAR; }