bytesPerLine
Defines BytesHexFormat.bytesPerLine of the format being built, Int.MAX_VALUE by default.
The value must be positive.
Refer to BytesHexFormat.bytesPerLine for details about how this format option affects the formatting and parsing results.
Since Kotlin
1.9Throws
if a non-positive value is assigned to this property.
Samples
import kotlin.test.*
fun main() {
//sampleStart
val data = ByteArray(7) { it.toByte() }
// By default, bytesPerLine is set to Int.MAX_VALUE, which exceeds data.size.
// Therefore, all bytes are formatted as a single line without any line breaks.
println(data.toHexString()) // 00010203040506
println("\"00010203040506\".hexToByteArray().contentEquals(data) is ${"00010203040506".hexToByteArray().contentEquals(data)}") // true
// Setting bytesPerLine to 3 splits the output into 3 lines with 3, 3, and 1 bytes, respectively.
// Each line is separated by a line feed (LF) character.
val threePerLineFormat = HexFormat {
bytes.bytesPerLine = 3
}
println(data.toHexString(threePerLineFormat)) // 000102\n030405\n06
// When parsing, any of the line separators CRLF, LF, and CR are accepted.
println("\"000102\\n030405\\r\\n06\".hexToByteArray(threePerLineFormat).contentEquals(data) is ${"000102\n030405\r\n06".hexToByteArray(threePerLineFormat).contentEquals(data)}") // true
// Parsing fails if the input string does not conform to specified format.
// In this case, lines do not consist of the expected number of bytes.
// "0001\n0203\n0405\n06".hexToByteArray(threePerLineFormat) // will fail with IllegalArgumentException
//sampleEnd
}