Numerics

This repository contains typealiases for Kotlin types as Byte, Short, Int and Long to their respective representations in Rust as Int8, Int16, Int32, Int64 and so on.

Type aliases

typealias UInt8 = UByte
typealias UInt16 = UShort
typealias UInt32 = UInt
typealias UInt64 = ULong

typealias Int8 = Byte
typealias Int16 = Short
typealias Int32 = Int
typealias Int64 = Long

All types are provided with to and from ByteArray conversion for our SCALE codec purpose using Little Endian system.

// Usage example
val int64Value: Int64 = 64L // don't forget L suffix for "long" types as we're still in Kotlin environment
val int64ByteArray: ByteArray = int64Value.toByteArray() // [64, 0, 0, 0, 0, 0, 0, 0]
val int64FromByteArray: Int64 = int64ByteArray.toInt64() // 64L, .toLong() also available

Big Integer wrapping types

Kotlin doesn't have integer with size bigger than 64 bits, thus we have added new types to this library which wrap BigInteger so that they can be used for separate serialization adapter in our SCALE codec library.

data class Int128(val value: BigInteger)
data class Int256(val value: BigInteger)
data class Int512(val value: BigInteger)

data class UInt128(val value: BigInteger)
data class UInt256(val value: BigInteger)
data class UInt512(val value: BigInteger)

Each of them also has same to and from ByteArray set of methods, but also for convenience there are constructors to avoid extra BigInteger construction.

// Usage example
val int256FromString = Int256("1234567890") // creates Int256 instance which wraps BigInteger with 1234567890 value inside it

val int8: Int8 = 8
val int256FromInt8 = Int256(int8) // creates Int256 instance wrapping BigInteger which is created from Int8 / Byte value

val int16: Int16 = 16
val int256FromInt16 = Int256(int16) // creates Int256 instance wrapping BigInteger which is created from Int16 / Short value

val int32: Int32 = 32
val int256FromInt32 = Int256(int32) // creates Int256 instance wrapping BigInteger which is created from Int32 / Int value

val int64: Int64 = 64
val int256FromInt64 = Int256(int64) // creates Int256 instance wrapping BigInteger which is created from Int64 / Long value

All of 128, 256, 512 unsigned and signed types allows you to instantiate object by providing any respectively unsigned or signed integers of lesser size, including BigInteger wrapping ones.

Last updated