[Kotlin][Java] ByteArrays๋ฅผ 16์ง์(Hex) String์ผ๋ก ๋ณํ
https://www.baeldung.com/kotlin/byte-arrays-to-hex-strings
์ฌ๊ธฐ์ ์ ๋ง ์ ๋์์.
๋ค์ํ ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค.
๊ทธ ์ค์์ ๋ค์์ ๋ฐฉ๋ฒ์ ์ฃผ๋ชฉํ๋ค.
https://www.baeldung.com/kotlin/byte-arrays-to-hex-strings
val hexChars = "0123456789abcdef".toCharArray()
fun ByteArray.toHex4(): String {
val hex = CharArray(2 * this.size)
this.forEachIndexed { i, byte ->
val unsigned = 0xff and byte.toInt()
hex[2 * i] = hexChars[unsigned / 16]
hex[2 * i + 1] = hexChars[unsigned % 16]
}
return hex.joinToString("")
}
//์ถ์ฒ : https://www.baeldung.com/kotlin/byte-arrays-to-hex-strings#loops-and-bitwise-operations
ํ๋ก์ ํธ๋ฅผ ๋ค์ง๋ค๊ฐ ๋ฐ๊ฒฌํ ์๋ฐ ์ฝ๋์์ ์ด๋ ๊ฒ ๊ตฌํ๋์ด ์๋ ๊ฒ๊ณผ ๊ฒฐ๊ตญ์ ๋์ผํ๋ค๋ ๊ฒ์ ์์๋ค.
public static int getUniqueId(...) {
...
byte[] hash;
...
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return ...
}
์ ์๋ฐ ์ฝ๋๋ฅผ ์ฝํ๋ฆฐ์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ์ํด
Integer.toHexString()์ ์ด๋ป๊ฒ ๋ณ๊ฒฝํ ๊ฒ์ธ๊ฐ?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-string.html
toString - Kotlin Programming Language
kotlinlang.org
fun Byte.toString(radix: Int) : String ์ ์ฌ์ฉํ๋ฉด ๋๊ฒ ๋ค.
> (b.toInt() & 0xFF).toString(16)
์ฝํ๋ฆฐ์์๋
byte.toInt() and 0xFF
์๋ฐ์์๋
byte & 0xFF
์ ์จ์ byte๋ฅผ ์์๋ก ํํ์ํจ๋ค. ์์ ๋ถํธ์ ๋นํธ๋ง ๋ค์ง๋๋ค.
https://www.baeldung.com/kotlin/byte-arrays-to-hex-strings#loops-and-bitwise-operations
์๊ธฐ์ ์ค๋ช ์ด ์ ๋์์๋ค.