alakazam.kotlin.core.ByteArrayExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-core Show documentation
Show all versions of kotlin-core Show documentation
A set of useful functions and extensions for Kotlin development.
@file:Suppress("TopLevelPropertyNaming")
package alakazam.kotlin.core
private const val MAX_BYTE = 0xff
private const val NUM_MAC_BYTES = 6
/**
* Returns a string of each byte represented as a two-character capitalised hexadecimal string, with
* an optional separating character between each byte. If the separator character is ignored or
* set to null, there will be no gap between each byte string.
*/
public fun ByteArray.toHexString(separator: String = ""): String = joinToString(separator) {
"%02X".format(it.toInt() and MAX_BYTE)
}
/**
* Converts the [ByteArray] to a string MAC address representation, e.g. "AB:CD:EF:12:34:56".
* This also accepts an optional separator parameter, in case you want hyphens or dots between each
* MAC byte instead of colons.
*/
public fun ByteArray.toMacAddress(separator: String = ":"): String {
if (size != NUM_MAC_BYTES) {
error("MAC addresses need 6 bytes, this has $size")
}
return toHexString(separator)
}