Best android open-source packages and libraries.

Endians

Big & Little Endian utils for Kotlin Multiplatform
Updated 1 month ago

endians

badge-license badge-latest-release

badge-kotlin

badge-platform-android badge-platform-jvm badge-platform-js badge-platform-js-node badge-platform-wasm badge-platform-linux badge-platform-macos badge-platform-ios badge-platform-tvos badge-platform-watchos badge-platform-windows badge-support-android-native badge-support-apple-silicon badge-support-js-ir badge-support-linux-arm

Big & Little Endian utils for Kotlin Multiplatform

Small library which adds 2 kotlin value classes, BigEndian and LittleEndian

Usage

fun main() {
    // Extension functions for converting Short, Int, or
    // Long to LittleEndian or BigEndian
    // e.g. Long.toLittleEndian()
    val le = 5L.toLittleEndian()
    val be = 5L.toBigEndian()
    
    println(le)
    // LE[5,0,0,0,0,0,0,0]
    println(be)
    // BE[0,0,0,0,0,0,0,5]

    println(le[0])
    // 5
    println(be[7])
    // 5
    
    // Helper functions to convert bytes to Short, Int, or Long
    val leLong = LittleEndian.bytesToLong(0, 0, 0, 5, 0, 0, 0, 10)
    println(leLong)
    // 720575940463165440
    val beLong = BigEndian.bytesToLong(0, 0, 0, 5, 0, 0, 0, 10)
    println(beLong)
    // 21474836490

    val bytes = ByteArray(le.size + be.size)
    le.copyInto(bytes)
    be.copyInto(bytes, le.size)
    
    // Safely convert BigEndian or LittleEndian to the desired
    // primitives w/o worrying about the size of the underlying
    // ByteArray
    be.toByte()
    be.toShort()
    be.toInt()
    be.toLong()
    
    // APIs for both classes are the same, with only 1 difference
    be.toLittleEndian() // Convert a BigEndian to a LittleEndian
    le.toBigEndian()    // Convert a LittleEndian to a BigEndian
}

Get Started

The best way to keep KotlinCrypto dependencies up to date is by using the version-catalog. Alternatively, see below.

// build.gradle.kts
dependencies {
    implementation("org.kotlincrypto.endians:endians:0.3.0")
}
Tags utils