byteSeparator
The string used to separate adjacent bytes within a group.
The number of bytes in each group is determined by the bytesPerGroup option.
When formatting, adjacent bytes within each group are separated by this string.
When parsing, adjacent bytes within each group must be separated by this string The parsing of this separator is performed in a case-insensitive manner.
Since Kotlin
1.9Samples
import kotlin.test.*
fun main() {
//sampleStart
val data = ByteArray(7) { it.toByte() }
// By default, the byteSeparator is an empty string, hence all bytes are concatenated without any separator.
println(data.toHexString()) // 00010203040506
println("\"00010203040506\".hexToByteArray().contentEquals(data) is ${"00010203040506".hexToByteArray().contentEquals(data)}") // true
// Specifying a custom byte separator, a colon in this case.
val colonByteSeparatorFormat = HexFormat { bytes.byteSeparator = ":" }
println(data.toHexString(colonByteSeparatorFormat)) // 00:01:02:03:04:05:06
println("\"00:01:02:03:04:05:06\".hexToByteArray(colonByteSeparatorFormat).contentEquals(data) is ${"00:01:02:03:04:05:06".hexToByteArray(colonByteSeparatorFormat).contentEquals(data)}") // true
// Only adjacent bytes within a group are separated by the byteSeparator.
val groupFormat = HexFormat {
bytes.bytesPerGroup = 3
bytes.byteSeparator = ":"
}
println(data.toHexString(groupFormat)) // 00:01:02 03:04:05 06
println("\"00:01:02 03:04:05 06\".hexToByteArray(groupFormat).contentEquals(data) is ${"00:01:02 03:04:05 06".hexToByteArray(groupFormat).contentEquals(data)}") // true
// Parsing fails due to incorrect byte separator.
// In this case, the input string is lacking the necessary byte separators within groups.
// "000102 030405 06".hexToByteArray(groupFormat) // will fail with IllegalArgumentException
//sampleEnd
}