bytesPerGroup
The maximum number of bytes per group in a line, Int.MAX_VALUE by default.
The number of bytes in each line is determined by the bytesPerLine option.
When formatting, each line is split into groups with bytesPerGroup bytes in each group, except for the last group in a line, which may have fewer bytes if the number of bytes in the line does not divide evenly by this value. Adjacent groups within each line are separated by groupSeparator. Note that if this value is greater than or equal to the number of bytes in a line, the bytes are not split into groups.
When parsing, each line in the input string must be split into groups of bytesPerGroup bytes, except for the last group in a line, which may have fewer bytes. Adjacent groups within each line must be separated by groupSeparator. The parsing of the separator is performed in a case-insensitive manner.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val data = ByteArray(7) { it.toByte() }
// By default, both bytesPerLine and bytesPerGroup are set to Int.MAX_VALUE, which exceeds data.size.
// Hence, all bytes are formatted as a single line and a single group.
println(data.toHexString()) // 00010203040506
println("\"00010203040506\".hexToByteArray().contentEquals(data) is ${"00010203040506".hexToByteArray().contentEquals(data)}") // true
// Setting bytesPerGroup to 2 with the default group separator, which is two spaces.
val twoPerGroupFormat = HexFormat {
bytes.bytesPerGroup = 2
}
println(data.toHexString(twoPerGroupFormat)) // 0001 0203 0405 06
println("\"0001 0203 0405 06\".hexToByteArray(twoPerGroupFormat).contentEquals(data) is ${"0001 0203 0405 06".hexToByteArray(twoPerGroupFormat).contentEquals(data)}") // true
// Specifying a custom group separator, a dot in this case.
val dotGroupSeparatorFormat = HexFormat {
bytes {
bytesPerGroup = 2
groupSeparator = "."
}
}
println(data.toHexString(dotGroupSeparatorFormat)) // 0001.0203.0405.06
println("\"0001.0203.0405.06\".hexToByteArray(dotGroupSeparatorFormat).contentEquals(data) is ${"0001.0203.0405.06".hexToByteArray(dotGroupSeparatorFormat).contentEquals(data)}") // true
// If bytesPerLine is less than or equal to bytesPerGroup, each line is treated as a single group,
// hence no group separator is used.
val lessBytesPerLineFormat = HexFormat {
bytes {
bytesPerLine = 3
bytesPerGroup = 4
groupSeparator = "|"
}
}
println(data.toHexString(lessBytesPerLineFormat)) // 000102\n030405\n06
println("\"000102\\n030405\\n06\".hexToByteArray(lessBytesPerLineFormat).contentEquals(data) is ${"000102\n030405\n06".hexToByteArray(lessBytesPerLineFormat).contentEquals(data)}") // true
// When bytesPerLine is greater than bytesPerGroup, each line is split into multiple groups.
val moreBytesPerLineFormat = HexFormat {
bytes {
bytesPerLine = 5
bytesPerGroup = 2
groupSeparator = "."
}
}
println(data.toHexString(moreBytesPerLineFormat)) // 0001.0203.04\n0506
println("\"0001.0203.04\\n0506\".hexToByteArray(moreBytesPerLineFormat).contentEquals(data) is ${"0001.0203.04\n0506".hexToByteArray(moreBytesPerLineFormat).contentEquals(data)}") // true
// Parsing fails due to incorrect group separator.
// "0001 0203 04\n0506".hexToByteArray(moreBytesPerLineFormat) // will fail with IllegalArgumentException
//sampleEnd
}