removeLeadingZeros

Common
JVM
JS
Native
1.0
val removeLeadingZeros: Boolean
(source)

Specifies whether to remove leading zeros in the hexadecimal representation of a numeric value, false by default.

The hexadecimal representation of a value is calculated by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting with the most significant bits.

When formatting, if this option is true and the length of the hexadecimal representation exceeds minLength, leading zeros are removed until the length matches minLength. If the length does not exceed minLength, this option has no effect on the formatting result.

When parsing, this option is ignored.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// By default, removeLeadingZeroes is `false`
println(0x3a.toHexString()) // 0000003a

val removeLeadingZerosFormat = HexFormat { number.removeLeadingZeros = true }

// If there are no leading zeros, removeLeadingZeroes has no effect
println(0x3a.toByte().toHexString(removeLeadingZerosFormat)) // 3a

// The leading zeros in the hex representation are removed until minLength is reached.
// By default, minLength is 1.
println(0x3a.toHexString(removeLeadingZerosFormat)) // 3a
println(0.toHexString(removeLeadingZerosFormat)) // 0

// Here minLength is set to 6.
val shorterLengthFormat = HexFormat {
    number.removeLeadingZeros = true
    number.minLength = 6
}
println(0x3a.toHexString(shorterLengthFormat)) // 00003a

// When minLength is longer than the hex representation, the hex representation is padded with zeros.
// removeLeadingZeros is ignored in this case.
val longerLengthFormat = HexFormat {
    number.removeLeadingZeros = true
    number.minLength = 12
}
println(0x3a.toHexString(longerLengthFormat)) // 00000000003a

// When parsing, removeLeadingZeros is ignored
println("0000003a".hexToInt(removeLeadingZerosFormat)) // 58
//sampleEnd
}