1. Overview

In this quick tutorial, we’re going to evaluate different available options to convert Strings to Longs and vice versa in Kotlin.

2. Basic Conversion

To convert any String to its Long equivalent, we can use the toLong() extension function:

val number = "42".toLong()
assertEquals(42, number)

This extension function with throw an instance of NumberFormatException, when the String isn’t a valid numeric value:

assertThrows<NumberFormatException> { "the answer is 42".toLong() }

As opposed to toLong(), the toLongOrNull() extension function will return null if the given String is not a valid number:

val number: Long? = "42".toLongOrNull()
assertEquals(42, number)

assertNull("the answer is 42".toLongOrNull())

As shown above, this function catches the thrown exception and returns null instead.

3. Radix Conversion

In addition to decimal numbers, we can convert Strings in other numeric systems such as hexadecimal, binary, or octal to Long instances. All we have to do is pass a radix to toLong(radix) or toLongOrNull(radix) functions:

assertEquals(15, "f".toLong(16))
assertEquals(15, "F".toLong(16))
assertEquals(15, "17".toLong(8))
assertEquals(15, "1111".toLongOrNull(2))

Similarly, if the given String isn’t a valid number in a particular numeric system, the toLong(radix) throws an exception. Moreover, the toLongOrNull(radix) returns null in these situations:

assertThrows<NumberFormatException> { "fg".toLong(16) }

Hexadecimal numbers can’t have the letter “g“, and octal numbers only contain numbers in the [1, 7] range, so both examples are invalid numbers.

4. Unsigned Long

As of Kotlin 1.3, Kotlin supports unsigned integers in addition to signed ones. Naturally, it’s also possible to convert Strings to these unsigned numbers, as well:

assertEquals(42uL, "42".toULong())

Similarly, we can target non-decimal systems:

assertEquals(15uL, "f".toULong(16))

Moreover, the unsigned conversions provide the same API as the signed ones for conversion:

assertEquals(15uL, "17".toULongOrNull(8))
assertThrows<NumberFormatException> { "179".toULong(8) }

Since the unsigned integers are still an experimental feature, we should use the ExperimentalUnsignedTypes annotation to suppress the compiler warnings.

5. Long to String

In addition to converting Strings to Longs, it’s possible to perform conversions in the opposite direction. In order to do that, we can use the toString() function:

assertEquals("42", 42.toString())

It’s even possible to convert a decimal Long to its corresponding String in other numeric systems:

assertEquals("101010", 42.toString(2))
assertEquals("2a", 42uL.toString(16))

Here we’re converting an unsigned and a signed Long to its binary and hexadecimal representations.

6. Conclusion

In this short tutorial, we saw how we could convert Long values to their corresponding String representations and vice versa. Along the way, we also did briefly talked about the new unsigned numbers introduced in Kotlin 1.3.

As usual, all the examples are available over on GitHub.