prefix

Common
JVM
JS
Native
1.0
val prefix: String
(source)

The string that immediately precedes the hexadecimal representation of a numeric value, empty string by default.

When formatting, this string is placed before the hexadecimal representation. When parsing, the string being parsed must start with this string. The parsing of this prefix is performed in a case-insensitive manner.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// By default, `prefix` is an empty string.
println(0x3a.toHexString()) // 0000003a
println("0000003a".hexToInt()) // 58

val prefixFormat = HexFormat { number.prefix = "0x" }

// `prefix` is placed before the hex representation.
println(0x3a.toHexString(prefixFormat)) // 0x0000003a
println("0x0000003a".hexToInt(prefixFormat)) // 58

// Parsing `prefix` is conducted in a case-insensitive manner.
println("0X0000003a".hexToInt(prefixFormat)) // 58

// Parsing fails if the input string does not start with the specified prefix.
// "0000003a".hexToInt(prefixFormat) // will fail with IllegalArgumentException
//sampleEnd
}