minLength

Defines NumberHexFormat.minLength of the format being built, 1 by default.

The value must be positive.

Refer to NumberHexFormat.minLength for details about how the format option affects the formatting and parsing results.

Since Kotlin

2.0

Throws

if a non-positive value is assigned to this property.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   // By default, minLength is 1 and removeLeadingZeros is false.
println(0x3a.toHexString()) // 0000003a

// Specifying a minLength shorter than the hex representation with removeLeadingZeros set to false.
println(0x3a.toHexString(HexFormat { number.minLength = 4 })) // 0000003a

// Specifying a minLength shorter than the hex representation with removeLeadingZeros set to true.
val shorterLengthFormat = HexFormat {
    number.removeLeadingZeros = true
    number.minLength = 4
}
println(0x3a.toHexString(shorterLengthFormat)) // 003a
println(0xff80ed.toHexString(shorterLengthFormat)) // ff80ed

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

// When parsing, minLength is ignored.
println("3a".hexToInt(longerLengthFormat)) // 58

// The number of hex digits can be greater than what can fit into the type.
println("00000000003a".hexToInt()) // 58
println("0000ffffffff".hexToInt()) // -1
// But excess leading digits must be zeros.
// "000100000000".hexToInt() // will fail with IllegalArgumentException 
   //sampleEnd
}