minglipro 450b4e1a93
feat(utils): 添加数组工具类和国际化支持
- 添加ByteArray.toHexString和String.toHexByteArray扩展函数
- 实现多种数组类型的copyTo和copyFrom扩展方法
- 在AutoConfiguration中集成I18N国际化功能
- 添加AutoService注解及处理器用于自动服务注册
- 新增BadGatewayException HTTP状态异常类
- 更新启动横幅格式并添加国际化文本支持
2026-02-08 03:17:14 +08:00

69 lines
2.2 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright 2026 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base10.kt
* LastUpdate 2026-02-08 03:08:10
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
import java.math.BigInteger
internal class Base10 : BaseCodec {
/**
* 将字节数组编码为Base10字符串十进制
*
* @param bytes 需要编码的字节数组
* @return 编码后的Base10字符串十进制数字
*/
override fun encode(bytes: ByteArray): String {
if (bytes.isEmpty()) return "0"
// 将字节数组转换为正的大整数
val bigInt = BigInteger(1, bytes) // 参数1表示正数
return bigInt.toString(10) // 转换为10进制字符串
}
/**
* 将Base10字符串解码为字节数组
*
* @param string 需要解码的Base10字符串十进制数字
* @return 解码后的字节数组
*/
override fun decode(string: String): ByteArray {
// 验证输入是否为有效的十进制数字
if (!string.matches(Regex("\\d+"))) {
throw IllegalArgumentException("Base10字符串只能包含数字0-9")
}
val bigInt = BigInteger(string, 10) // 从10进制解析
// 转换为字节数组,并确保保留前导零
var byteArray = bigInt.toByteArray()
// BigInteger.toByteArray() 可能会添加一个符号字节
// 对于正数如果第一个字节是0需要移除它
if (byteArray.isNotEmpty() && byteArray[0] == 0.toByte()) {
byteArray = byteArray.copyOfRange(1, byteArray.size)
}
return byteArray
}
}