Builder
Provides an API for building a HexFormat.
This class is a builder for HexFormat, and
serves as the receiver type of the lambda expression used in the HexFormat { }
builder function to create a
new format. Each option in this class corresponds to an option in HexFormat and defines it in the resulting
format. For example, use val format = HexFormat { bytes.byteSeparator = ":" }
to set
BytesHexFormat.byteSeparator option of the HexFormat.bytes property.
Refer to HexFormat for details about how the configured format options affect formatting and parsing results.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
// Specifying format options for numeric values.
println(58.toHexString(HexFormat { number.removeLeadingZeros = true })) // 3a
println("0x3a".hexToInt(HexFormat { number.prefix = "0x" })) // 58
// Specifying format options for byte arrays.
val macAddressFormat = HexFormat {
upperCase = true
bytes {
bytesPerGroup = 2
groupSeparator = "."
}
}
val macAddressBytes = byteArrayOf(0x00, 0x1b, 0x63, 0x84.toByte(), 0x45, 0xe6.toByte())
println(macAddressBytes.toHexString(macAddressFormat)) // 001B.6384.45E6
println("\"001B.6384.45E6\".hexToByteArray(macAddressFormat).contentEquals(macAddressBytes) is ${"001B.6384.45E6".hexToByteArray(macAddressFormat).contentEquals(macAddressBytes)}") // true
// Creating a format that defines options for both byte arrays and numeric values.
val customHexFormat = HexFormat {
upperCase = true
number {
removeLeadingZeros = true
prefix = "0x"
}
bytes {
bytesPerGroup = 2
groupSeparator = "."
}
}
// Formatting numeric values utilizes the `upperCase` and `number` options.
println(58.toHexString(customHexFormat)) // 0x3A
// Formatting byte arrays utilizes the `upperCase` and `bytes` options.
println(macAddressBytes.toHexString(customHexFormat)) // 001B.6384.45E6
//sampleEnd
}
Properties
bytes
Defines HexFormat.bytes of the format being built.
val bytes: Builder
number
Defines HexFormat.number of the format being built.
val number: Builder
upperCase
Defines HexFormat.upperCase of the format being built, false
by default.
var upperCase: Boolean