Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public class BaseConvertUtil {
  2.  
  3. final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  4. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  5.  
  6. public static void main(String args[]) {
  7.  
  8. System.out.println("test 10(10) do 2:"+convert("10",10,2));
  9. }
  10.  
  11. public static String convert(String num, int radixFrom, int radixTo) {
  12. return convertHelperStr(convertHelperInt(num, radixFrom), radixTo);
  13. }
  14.  
  15. public static String convertHelperStr(int i,int radix) {
  16.  
  17. if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  18. radix = 10;
  19.  
  20. /* Use the faster version */
  21. if (radix == 10) {
  22. return Integer.toString(i);
  23. }
  24.  
  25. char buf[] = new char[33];
  26. boolean negative = (i < 0);
  27. int charPos = 32;
  28.  
  29. if (!negative) {
  30. i = -i;
  31. }
  32.  
  33. while (i <= -radix) {
  34. buf[charPos--] = digits[-(i % radix)];
  35. i = i / radix;
  36. }
  37. buf[charPos] = digits[-i];
  38.  
  39. if (negative) {
  40. buf[--charPos] = '-';
  41. }
  42.  
  43. return new String(buf, charPos, (33 - charPos));
  44. }
  45.  
  46. public static int convertHelperInt(String s, int radix) {
  47.  
  48. if (s == null) {
  49. throw new NumberFormatException("null");
  50. }
  51.  
  52. if (radix < Character.MIN_RADIX) {
  53. throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
  54. }
  55.  
  56. if (radix > Character.MAX_RADIX) {
  57. throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
  58. }
  59.  
  60. int result = 0;
  61. boolean negative = false;
  62. int i = 0, len = s.length();
  63. int limit = -Integer.MAX_VALUE;
  64. int multmin;
  65. int digit;
  66.  
  67. if (len > 0) {
  68. char firstChar = s.charAt(0);
  69. if (firstChar < '0') { // Possible leading "+" or "-"
  70. if (firstChar == '-') {
  71. negative = true;
  72. limit = Integer.MIN_VALUE;
  73. } else if (firstChar != '+')
  74. throw new NumberFormatException(s);
  75.  
  76. if (len == 1) // Cannot have lone "+" or "-"
  77. throw new NumberFormatException(s);
  78. i++;
  79. }
  80. multmin = limit / radix;
  81. while (i < len) {
  82. // Accumulating negatively avoids surprises near MAX_VALUE
  83. digit = Character.digit(s.charAt(i++), radix);
  84. if (digit < 0) {
  85. throw new NumberFormatException(s);
  86. }
  87. if (result < multmin) {
  88. throw new NumberFormatException(s);
  89. }
  90. result *= radix;
  91. if (result < limit + digit) {
  92. throw new NumberFormatException(s);
  93. }
  94. result -= digit;
  95. }
  96. } else {
  97. throw new NumberFormatException(s);
  98. }
  99.  
  100. return negative ? result : -result;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement