finish web test api

This commit is contained in:
2025-03-31 20:24:26 +08:00
parent f455af3589
commit 0da5b85173
10 changed files with 392 additions and 59 deletions

83
server/src/Common.cs Normal file
View File

@@ -0,0 +1,83 @@
using DotNext;
namespace Common
{
class NumberProcessor
{
public static Result<byte[]> NumberToBytes(ulong num, uint length, bool isRightHigh = false)
{
if (length > 8)
{
throw new ArgumentException(
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(length)
);
}
var arr = new byte[length];
if (isRightHigh)
{
for (var i = 0; i < length; i++)
{
arr[length - 1 - i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
}
}
else
{
for (var i = 0; i < length; i++)
{
arr[i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
}
}
return arr;
}
public static Result<ulong> BytesToNumber(byte[] bytes, bool isRightLeft = false)
{
if (bytes.Length > 8)
{
throw new ArgumentException(
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(bytes)
);
}
ulong num = 0;
int len = bytes.Length;
if (isRightLeft)
{
for (var i = 0; i < len; i++)
{
num += Convert.ToUInt64(bytes[len - 1 - i] << (i << 3));
}
}
else
{
for (var i = 0; i < len; i++)
{
num += Convert.ToUInt64(bytes[i] << (i << 3));
}
}
return num;
}
public static Result<byte[]> StringToBytes(string str, int numBase = 16)
{
var len = str.Length;
var bytesLen = len / 2;
var bytes = new byte[bytesLen];
for (var i = 0; i < bytesLen; i++)
{
bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
}
return bytes;
}
}
}