package com.grkj.iscs.extentions

/**
 * 长度不足默认补0
 */
fun String.toByteArray(length: Int, paddingByte: Byte = 0): ByteArray {
    val bytes = this.toByteArray(Charsets.UTF_8)
    return if (bytes.size >= length) {
        bytes.copyOf(length)
    } else {
        ByteArray(length) { index ->
            if (index < bytes.size) {
                bytes[index]
            } else {
                paddingByte
            }
        }
    }
}

/**
 * 移除开头的补0，长度不足8位补0
 */
fun String.removeLeadingZeros(): String {
    val trimmed = this.dropWhile { it == '0' }.takeIf { it.isNotEmpty() } ?: "0"
    return trimmed.padStart(8, '0')
}