사용자 도구

사이트 도구


java:number

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:number [2015/09/24 16:55]
kwon37xi
java:number [2021/02/17 00:09] (현재)
kwon37xi
줄 56: 줄 56:
 String hex = String.format("%016x", value); String hex = String.format("%016x", value);
 </code> </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.1443083149.txt.gz · 마지막으로 수정됨: 2015/09/24 16:55 저자 kwon37xi