байтов в строке
Начиная с Kotlin: 2.2
var bytesPerLine: Int
Задаёт BytesHexFormat.bytesPerLine для создаваемого формата; по умолчанию — Int.MAX_VALUE.
Значение должно быть положительным.
См. BytesHexFormat.bytesPerLine, чтобы узнать, как этот параметр формата влияет на результаты форматирования и разбора.
Исключения
если этому свойству присвоено неположительное значение.
Примеры
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
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/-hex-format/-bytes-hex-format/-builder/bytes-per-line.html