package com.grkj.iscs_mc.extentions

//fun Int.toByteArray(capability: Int = 2): ByteArray {
//    return ByteBuffer.allocate(capability)
//        .order(ByteOrder.BIG_ENDIAN) // 可以根据需要改为 ByteOrder.LITTLE_ENDIAN
//        .putShort(this.toShort()) // 只取低16位
//        .array()
//}

fun Int.toByteArray(capability: Int = 2): 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
}