문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:number [2015/09/24 16:55] kwon37xi |
java:number [2021/02/17 00:09] kwon37xi |
||
---|---|---|---|
줄 56: | 줄 56: | ||
String hex = String.format(" | String hex = String.format(" | ||
</ | </ | ||
+ | * [[https:// | ||
+ | * Apache Commons Codec | ||
+ | <code java> | ||
+ | import org.apache.commons.codec.binary.Hex; | ||
+ | ... | ||
+ | Hex.encodeHexString(someByteArray)); | ||
+ | </ | ||
+ | * 순수 Java | ||
+ | <code java> | ||
+ | byte bytes[] = {(byte)0, (byte)0, (byte)134, (byte)0, (byte)61}; | ||
+ | String hex = javax.xml.bind.DatatypeConverter.printHexBinary(bytes); | ||
+ | </ | ||
+ | |||
+ | * 순수 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; | ||
+ | int v = bytes[j] & 0xFF; | ||
+ | hexChars[j * 2] = HEX_ARRAY[v >>> | ||
+ | hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
+ | } | ||
+ | return new String(hexChars); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * 순수 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; | ||
+ | int v = bytes[j] & 0xFF; | ||
+ | hexChars[j * 2] = HEX_ARRAY[v >>> | ||
+ | hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
+ | } | ||
+ | return new String(hexChars, | ||
+ | } | ||
+ | </ |