사용자 도구

사이트 도구


java:number

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:number [2015/07/13 11:50]
kwon37xi
java:number [2021/02/17 00:09] (현재)
kwon37xi
줄 31: 줄 31:
         return new BigDecimal(number.longValue());         return new BigDecimal(number.longValue());
     if(number instanceof Float || number instanceof Double)     if(number instanceof Float || number instanceof Double)
-        return new BigDecimal(number.doubleValue());+        // float,double은 toString을 통해 변환하는 것이 정확도가 더 높았다. 
 +        return new BigDecimal(number.toString());
  
     try {     try {
줄 43: 줄 44:
 </code> </code>
   * ''isSpecial''이 없으면 ''Float''과 ''Double''의 NaN, infinite 상태에서 ''NumberFormatException''이 발생한다.   * ''isSpecial''이 없으면 ''Float''과 ''Double''의 NaN, infinite 상태에서 ''NumberFormatException''이 발생한다.
 +  * 비교 대상이 되는 두 객체가 동일 클래스 인스턴스라면 ''BigDecimal''로 변환하지 말고 즉시 비교하고 리턴하는 코드를 추가할 것. 그러면 불필요한 객체 생성 변환이 없어 성능향상이 될 것으로 보인다.
   * 특수한 숫자 객체를 제외한 int 계열의 숫자는 바로 ''BigDecimal''로 변환한다.   * 특수한 숫자 객체를 제외한 int 계열의 숫자는 바로 ''BigDecimal''로 변환한다.
 +
 +===== Hex 16진수 =====
 +  * 숫자를 16진수로 바꾸기
 +  * [[http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#toHexString%28long%29|Long.toHexString()]] 같은 메소드를 사용하면 된다. Integer 등에도 존재함.
 +  * 그러나 hex는 보통 2자리씩 이뤄지는데 때로 앞자리가 0으로 시작하면 홀수자리 hex 문자열이 나올 수도 있다. 이때, [[http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29|String.format()]]을 사용한다. [[http://stackoverflow.com/questions/8689526/integer-to-two-digits-hex-in-java|Integer to two digits hex in Java]]
 +
 +<code java>
 +// Long 값을 16자리 Hex 문자열로
 +String hex = String.format("%016x", value);
 +</code>
 +  * [[https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java|bytearray - How to convert a byte array to a hex string in Java? - Stack Overflow]] ''byte[]''를 hex 문자열로 변환하는 다양한 방법들
 +    * Apache Commons Codec
 +<code java>
 +import org.apache.commons.codec.binary.Hex;
 +...
 +Hex.encodeHexString(someByteArray));
 +</code>
 +  * 순수 Java
 +
 +<code java>
 +byte bytes[] = {(byte)0, (byte)0, (byte)134, (byte)0, (byte)61};
 +String hex = javax.xml.bind.DatatypeConverter.printHexBinary(bytes);
 +</code>
 +
 +  * 순수 Java - Java 8 이전
 +<code java>
 +public static String bytesToHex(byte[] bytes) {
 +    char[] hexChars = new char[bytes.length * 2];
 +    for (int j = 0; j < bytes.length; j++) {
 +        int v = bytes[j] & 0xFF;
 +        hexChars[j * 2] = HEX_ARRAY[v >>> 4];
 +        hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
 +    }
 +    return new String(hexChars);
 +}
 +</code>
 +
 +  * 순수 Java - Java 9 이후
 +<code java>
 +public static String bytesToHex(byte[] bytes) {
 +    byte[] hexChars = new byte[bytes.length * 2];
 +    for (int j = 0; j < bytes.length; j++) {
 +        int v = bytes[j] & 0xFF;
 +        hexChars[j * 2] = HEX_ARRAY[v >>> 4];
 +        hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
 +    }
 +    return new String(hexChars, StandardCharsets.UTF_8);
 +}
 +</code>
java/number.1436755817.txt.gz · 마지막으로 수정됨: 2015/07/13 11:50 저자 kwon37xi