fix bug: couldn't convert package

This commit is contained in:
2025-04-17 14:25:37 +08:00
parent 3b674f413a
commit d25f9882fc
7 changed files with 187 additions and 45 deletions

View File

@@ -2,8 +2,18 @@ using DotNext;
namespace Common
{
public class NumberProcessor
/// <summary>
/// 数字处理工具
/// </summary>
public class Number
{
/// <summary>
/// 整数转成二进制字节数组
/// </summary>
/// <param name="num">整数</param>
/// <param name="length">整数长度</param>
/// <param name="isRightHigh">是否高位在右边</param>
/// <returns>二进制字节数组</returns>
public static Result<byte[]> NumberToBytes(ulong num, uint length, bool isRightHigh = false)
{
if (length > 8)
@@ -33,6 +43,12 @@ namespace Common
return arr;
}
/// <summary>
/// 二进制字节数组转成整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <param name="isRightHigh">是否高位在右边</param>
/// <returns>整数</returns>
public static Result<ulong> BytesToNumber(byte[] bytes, bool isRightHigh = false)
{
if (bytes.Length > 8)
@@ -66,12 +82,28 @@ namespace Common
/// <summary>
/// 比特合并成二进制字节
/// </summary>
/// <param name="bits1">[TODO:parameter]</param>
/// <param name="bits1Len">[TODO:parameter]</param>
/// <param name="bits2">[TODO:parameter]</param>
/// <param name="bits2Len">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static Result<byte[]> MultiBitsToBytes(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
{
return NumberToBytes(MultiBitsToNumber(bits1, bits1Len, bits2, bits2Len).Value,
(bits1Len + bits2Len) % 8 != 0 ? (bits1Len + bits2Len) / 8 + 1 : (bits1Len + bits2Len) / 8);
}
/// <summary>
/// 比特合并成整型
/// </summary>
/// <param name="bits1">[TODO:parameter]</param>
/// <param name="bits1Len">[TODO:parameter]</param>
/// <param name="bits2">[TODO:parameter]</param>
/// <param name="bits2Len">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static Result<ulong> MultiBitsToNumber(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) throw new ArgumentException("Two Bits is more than 64 bits");
@@ -80,6 +112,14 @@ namespace Common
return num;
}
/// <summary>
/// 比特合并成整型
/// </summary>
/// <param name="bits1">[TODO:parameter]</param>
/// <param name="bits1Len">[TODO:parameter]</param>
/// <param name="bits2">[TODO:parameter]</param>
/// <param name="bits2Len">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static Result<uint> MultiBitsToNumber(uint bits1, uint bits1Len, uint bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) throw new ArgumentException("Two Bits is more than 64 bits");
@@ -88,17 +128,37 @@ namespace Common
return num;
}
/// <summary>
/// 比特位检查
/// </summary>
/// <param name="srcBits">[TODO:parameter]</param>
/// <param name="dstBits">[TODO:parameter]</param>
/// <param name="mask">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static bool BitsCheck(ulong srcBits, ulong dstBits, ulong mask = 0xFFFF_FFFF_FFFF_FFFF)
{
return (srcBits & mask) == dstBits;
}
/// <summary>
/// 比特位检查
/// </summary>
/// <param name="srcBits">[TODO:parameter]</param>
/// <param name="dstBits">[TODO:parameter]</param>
/// <param name="mask">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static bool BitsCheck(uint srcBits, uint dstBits, uint mask = 0xFFFF_FFFF)
{
return (srcBits & mask) == dstBits;
}
/// <summary>
/// 字符串转二进制字节数组
/// </summary>
/// <param name="str">[TODO:parameter]</param>
/// <param name="numBase">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public static byte[] StringToBytes(string str, int numBase = 16)
{
var len = str.Length;