All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alakazam.kotlin.core.ByteArrayExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.7.0
Show newest version
@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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy