Java2 - PHP base_convert() 함수 JAVA 변환
목록 추천  
추천수:
제 목 PHP base_convert() 함수 JAVA 변환
작성자 박세청 작성일 2014/02/26 14:07


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

 
}





이전글 자바 오브젝트 생성에 관한 문제 인데요...
다음글 [데이터형] double 데이터타입 연산 문제

목록 추천