Compare commits

..

No commits in common. "58378851bb20f9914c0f75e851f01e33b508c2b6" and "d2508f64840a5f28f972b0869aa6b2cda94e6b40" have entirely different histories.

4 changed files with 58 additions and 223 deletions

View File

@ -35,133 +35,57 @@ public class NumberTest
} }
/// <summary> /// <summary>
/// 测试 BytesToUInt64 的正常与异常情况,覆盖不同参数组合 /// 测试 BytesToUInt64 的正常与异常情况
/// </summary> /// </summary>
[Fact] [Fact]
public void Test_BytesToUInt64() public void Test_BytesToUInt64()
{ {
// 正常大端isLowNumHigh=false // 正常大端
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 }; var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
var result = Number.BytesToUInt64((byte[])bytes.Clone()); var result = Number.BytesToUInt64((byte[])bytes.Clone(), false);
Assert.True(result.IsSuccessful); Assert.True(result.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result.Value); Assert.Equal(0x12345678ABCDEF01UL, result.Value);
// 正常小端isLowNumHigh=true // 正常小端
var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 }; var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 };
var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true); var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true);
Assert.True(result2.IsSuccessful); Assert.True(result2.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result2.Value); Assert.Equal(0x12345678ABCDEF01UL, result2.Value);
// 长度不足8字节numLength=4大端 // 异常:长度超限
var bytes3 = new byte[] { 0x12, 0x34, 0x56, 0x78 }; var result3 = Number.BytesToUInt64(new byte[9], false);
var result3 = Number.BytesToUInt64((byte[])bytes3.Clone(), 0, 4, false); Assert.False(result3.IsSuccessful);
Assert.True(result3.IsSuccessful);
Assert.Equal(0x1234567800000000UL, result3.Value);
// 长度不足8字节numLength=4小端 // 异常不足8字节
var bytes4 = new byte[] { 0x78, 0x56, 0x34, 0x12 }; var result4 = Number.BytesToUInt64(new byte[] { 0x01, 0x02 }, false);
var result4 = Number.BytesToUInt64((byte[])bytes4.Clone(), 0, 4, true); Assert.False(result4.IsSuccessful); // BitConverter.ToUInt64 需要8字节
Assert.True(result4.IsSuccessful);
Assert.Equal(0x12345678UL, result4.Value);
// numLength=0
var bytes5 = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result5 = Number.BytesToUInt64((byte[])bytes5.Clone(), 0, 0, false);
Assert.True(result5.IsSuccessful);
Assert.Equal(0UL, result5.Value);
// offset测试
var bytes6 = new byte[] { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
var result6 = Number.BytesToUInt64(bytes6, 2, 8, false);
Assert.True(result6.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result6.Value);
// numLength超限>8应返回异常
var bytes7 = new byte[9];
var result7 = Number.BytesToUInt64(bytes7, 0, 9, false);
Assert.False(result7.IsSuccessful);
// offset+numLength超限
var bytes8 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
var result8 = Number.BytesToUInt64(bytes8, 2, 4, false);
Assert.True(result8.IsSuccessful);
Assert.Equal(0x0304000000000000UL, result8.Value);
// bytes长度不足offset+numLength
var bytes9 = new byte[] { 0x01, 0x02 };
var result9 = Number.BytesToUInt64(bytes9, 1, 2, true);
Assert.True(result9.IsSuccessful);
Assert.Equal(0x02UL, result9.Value);
// 空数组
var result10 = Number.BytesToUInt64(new byte[0], 0, 0, false);
Assert.True(result10.IsSuccessful);
Assert.Equal(0UL, result10.Value);
} }
/// <summary> /// <summary>
/// 测试 BytesToUInt32 的正常与异常情况,覆盖不同参数组合 /// 测试 BytesToUInt32 的正常与异常情况
/// </summary> /// </summary>
[Fact] [Fact]
public void Test_BytesToUInt32() public void Test_BytesToUInt32()
{ {
// 正常大端isLowNumHigh=false // 正常大端
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 }; var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result = Number.BytesToUInt32((byte[])bytes.Clone()); var result = Number.BytesToUInt32((byte[])bytes.Clone(), false);
Assert.True(result.IsSuccessful); Assert.True(result.IsSuccessful);
Assert.Equal(0x12345678U, result.Value); Assert.Equal(0x12345678U, result.Value);
// 正常小端isLowNumHigh=true // 正常小端
var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 }; var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 };
var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true); var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true);
Assert.True(result2.IsSuccessful); Assert.True(result2.IsSuccessful);
Assert.Equal(0x12345678U, result2.Value); Assert.Equal(0x12345678U, result2.Value);
// 长度不足4字节numLength=2大端 // 异常:长度超限
var bytes3 = new byte[] { 0x12, 0x34 }; var result3 = Number.BytesToUInt32(new byte[5], false);
var result3 = Number.BytesToUInt32((byte[])bytes3.Clone(), 0, 2, false); Assert.False(result3.IsSuccessful);
Assert.True(result3.IsSuccessful);
Assert.Equal(0x12340000U, result3.Value);
// 长度不足4字节numLength=2小端 // 异常不足4字节
var bytes4 = new byte[] { 0x34, 0x12 }; var result4 = Number.BytesToUInt32(new byte[] { 0x01, 0x02 }, false);
var result4 = Number.BytesToUInt32((byte[])bytes4.Clone(), 0, 2, true); Assert.False(result4.IsSuccessful); // BitConverter.ToUInt32 需要4字节
Assert.True(result4.IsSuccessful);
Assert.Equal(0x1234U, result4.Value);
// numLength=0
var bytes5 = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result5 = Number.BytesToUInt32((byte[])bytes5.Clone(), 0, 0, false);
Assert.True(result5.IsSuccessful);
Assert.Equal(0U, result5.Value);
// offset测试
var bytes6 = new byte[] { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78 };
var result6 = Number.BytesToUInt32(bytes6, 2, 4, false);
Assert.True(result6.IsSuccessful);
Assert.Equal(0x12345678U, result6.Value);
// numLength超限>4应返回异常
var bytes7 = new byte[5];
var result7 = Number.BytesToUInt32(bytes7, 0, 5, false);
Assert.False(result7.IsSuccessful);
// offset+numLength超限
var bytes8 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
var result8 = Number.BytesToUInt32(bytes8, 2, 2, false);
Assert.True(result8.IsSuccessful);
Assert.Equal(0x03040000U, result8.Value);
// bytes长度不足offset+numLength
var bytes9 = new byte[] { 0x01, 0x02 };
var result9 = Number.BytesToUInt32(bytes9, 1, 1, true);
Assert.True(result9.IsSuccessful);
Assert.Equal(0x02U, result9.Value);
// 空数组
var result10 = Number.BytesToUInt32(new byte[0], 0, 0, false);
Assert.True(result10.IsSuccessful);
Assert.Equal(0U, result10.Value);
} }
/// <summary> /// <summary>

View File

@ -11,7 +11,6 @@
<SpaRoot>../</SpaRoot> <SpaRoot>../</SpaRoot>
<SpaProxyServerUrl>http://localhost:5173</SpaProxyServerUrl> <SpaProxyServerUrl>http://localhost:5173</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm run dev</SpaProxyLaunchCommand> <SpaProxyLaunchCommand>npm run dev</SpaProxyLaunchCommand>
<NoWarn>CS1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -78,56 +78,6 @@ public class Number
return arr; return arr;
} }
/// <summary>
/// 二进制字节数组转成64bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <param name="offset">字节数组偏移量</param>
/// <param name="numLength">字节数组长度</param>
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
/// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes, int offset = 0, int numLength = 8, bool isLowNumHigh = false)
{
if (bytes.Length < offset)
{
return new(new ArgumentException($"The Length of bytes is less than offset"));
}
if (numLength > 8)
{
return new(new ArgumentException($"The Length of bytes is greater than 8"));
}
if (numLength <= 0) return 0;
try
{
byte[] numBytes = new byte[8]; // 8字节
int copyLen = Math.Min(numLength, bytes.Length - offset);
if (isLowNumHigh)
{
// 小端:拷贝到低位
Buffer.BlockCopy(bytes, offset, numBytes, 0, copyLen);
}
else
{
// 大端:拷贝到高位
byte[] temp = new byte[copyLen];
Buffer.BlockCopy(bytes, offset, temp, 0, copyLen);
Array.Reverse(temp);
Buffer.BlockCopy(temp, 0, numBytes, 8 - copyLen, copyLen);
}
UInt64 num = BitConverter.ToUInt64(numBytes, 0);
return num;
}
catch (Exception error)
{
return new(error);
}
}
/// <summary> /// <summary>
/// 二进制字节数组转成64bits整数 /// 二进制字节数组转成64bits整数
/// </summary> /// </summary>
@ -136,78 +86,25 @@ public class Number
/// <returns>整数</returns> /// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false) public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false)
{ {
return BytesToUInt64(bytes, 0, 8, isLowNumHigh); if (bytes.Length > 8)
}
/// <summary>
/// 二进制字节数组转成64bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes)
{
return BytesToUInt64(bytes, 0, 8, false);
}
/// <summary>
/// 二进制字节数组转成32bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <param name="offset">字节数组偏移量</param>
/// <param name="numLength">整形所占字节数组长度</param>
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
/// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes, int offset = 0, int numLength = 4, bool isLowNumHigh = false)
{
if (bytes.Length < offset)
{ {
return new(new ArgumentException($"The Length of bytes is less than offset")); return new(new ArgumentException(
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(bytes)
));
} }
if (numLength > 4) UInt64 num = 0;
{ int len = bytes.Length;
return new(new ArgumentException($"The Length of bytes is greater than 4"));
}
if (bytes.Length < offset)
{
return new(new ArgumentException($"The Length of bytes is less than offset"));
}
if (numLength > 4)
{
return new(new ArgumentException($"The Length of bytes is greater than 4"));
}
if (numLength <= 0) return 0;
try try
{ {
byte[] numBytes = new byte[4]; // 4字节 if (!isLowNumHigh)
int copyLen = Math.Min(numLength, bytes.Length - offset);
if (copyLen < 0) copyLen = 0;
if (isLowNumHigh)
{ {
// 小端:拷贝到低位 Array.Reverse(bytes);
if (copyLen > 0)
{
Buffer.BlockCopy(bytes, offset, numBytes, 0, copyLen);
}
}
else
{
// 大端:拷贝到高位
if (copyLen > 0)
{
byte[] temp = new byte[copyLen];
Buffer.BlockCopy(bytes, offset, temp, 0, copyLen);
Array.Reverse(temp);
Buffer.BlockCopy(temp, 0, numBytes, 4 - copyLen, copyLen);
}
} }
num = BitConverter.ToUInt64(bytes, 0);
UInt32 num = BitConverter.ToUInt32(numBytes, 0);
return num; return num;
} }
catch (Exception error) catch (Exception error)
@ -224,17 +121,32 @@ public class Number
/// <returns>整数</returns> /// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false) public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false)
{ {
return BytesToUInt32(bytes, 0, 4, isLowNumHigh); if (bytes.Length > 4)
} {
return new(new ArgumentException(
"Unsigned long number can't over 4 bytes(32 bits).",
nameof(bytes)
));
}
UInt32 num = 0;
int len = bytes.Length;
try
{
if (!isLowNumHigh)
{
Array.Reverse(bytes);
}
num = BitConverter.ToUInt32(bytes, 0);
return num;
}
catch (Exception error)
{
return new(error);
}
/// <summary>
/// 二进制字节数组转成32bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes)
{
return BytesToUInt32(bytes, 0, 4, false);
} }
/// <summary> /// <summary>

View File

@ -21,7 +21,7 @@ public class ProgressReporter : ProgressInfo, IProgress<int>
set set
{ {
_stepProgress = value; _stepProgress = value;
_expectedSteps = MaxProgress / value; ExpectedSteps = MaxProgress / value;
} }
} }
public int ExpectedSteps public int ExpectedSteps
@ -31,7 +31,7 @@ public class ProgressReporter : ProgressInfo, IProgress<int>
{ {
_expectedSteps = value; _expectedSteps = value;
MaxProgress = Number.IntPow(10, Number.GetLength(value)); MaxProgress = Number.IntPow(10, Number.GetLength(value));
_stepProgress = MaxProgress / value; StepProgress = MaxProgress / value;
} }
} }
public Func<int, Task>? ReporterFunc { get; set; } = null; public Func<int, Task>? ReporterFunc { get; set; } = null;