toByteOrNull

Parses the string to a Byte number or returns null if the string is not a valid representation of a Byte.

The string must consist of an optional leading + or - sign and decimal digits (0-9), and fit the valid Byte value range (within Byte.MIN_VALUE..Byte.MAX_VALUE), otherwise null is returned.

Since Kotlin

1.1

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   println("0".toByteOrNull()) // 0
println("42".toByteOrNull()) // 42
println("042".toByteOrNull()) // 42
println("-42".toByteOrNull()) // -42
// Byte.MAX_VALUE
println("127".toByteOrNull()) // 127
// Byte overflow
println("128".toByteOrNull()) // null
// 'a' is not a digit
println("-1a".toByteOrNull()) // null
// underscore
println("1_00".toByteOrNull()) // null
// whitespaces
println(" 22 ".toByteOrNull()) // null 
   //sampleEnd
}

Parses the string as a signed Byte number and returns the result or null if the string is not a valid representation of a number.

Since Kotlin

1.1

Throws

when radix is not a valid radix for string to number conversion.