byteSeparator

Defines BytesHexFormat.byteSeparator of the format being built, empty string by default.

The string must not contain line feed (LF) and carriage return (CR) characters.

Refer to BytesHexFormat.byteSeparator for details about how this format option affects the formatting and parsing results.

Since Kotlin

1.9

Throws

if a string containing LF or CR character is assigned to this property.

Samples

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
}