PHP base_convert() 함수 JAVA 변환
public class Util {
/**
* Converts a string from one base to another base. The bases must be between
* 2 and 36 inclusive. This function exhibits the same issues as the original
* PHP version related to overflow in 32 bit integers.
*
* @param inputValue A string representation of a number.
* @param fromBase The starting radix of a number between 2 and 36 inclusive.
* @param toBase The ending radix of the number between 2 and 36 inclusive.
* @return The <code>inputValue</code> converted into <code>toBase</code> base.
* @see http://www.php.net/manual/en/function.base-convert.php
*/
public static String base_convert(final String inputValue, final int fromBase, final int toBase) {
if (fromBase < 2 || fromBase > 36 || toBase < 2 || toBase > 36) {
return null;
}
String ret = null;
try {
ret = Integer.toString(Integer.parseInt(inputValue, fromBase), toBase);
} catch(Exception ex) {
ex.printStackTrace();
};
return ret;
}
/**
* Converts a decimal string into a binary string.
*
* @param inputValue A string representation of a decimal number.
* @return A bit string representation of the <code>inputValue</code>.
*/
public static String decbin(final String inputValue) {
return base_convert(inputValue, 10, 2);
}
/**
* Converts a binary string into a decimal string.
*
* @param inputValue A string representation of a binary number.
* @return A decimal number string representation of the <code>inputValue</code>.
*/
public static String bindec(final String inputValue) {
return base_convert(inputValue, 2, 10);
}
}