package com.grkj.iscs_mc.extentions

import java.nio.ByteBuffer
import java.nio.ByteOrder

fun Long.toByteArray(): ByteArray {
    return ByteBuffer.allocate(java.lang.Long.BYTES)
        .order(ByteOrder.LITTLE_ENDIAN)
        .putLong(this).array()
}

fun Long.toByteArrays(capability: Int = 4): ByteArray {
//    require(capability in 1..4) { "Length must be between 1 and 4" }
    val bytes = ByteArray(capability)
    for (i in 0 until capability) {
//        bytes[capability - i - 1] = ((this ushr (i * 8)) and 0xFF).toByte() // 大端模式
        bytes[i] = ((this ushr (i * 8)) and 0xFF).toByte()  // 小端模式
    }
    return bytes
}