add home view and reverse 4 bytes order to send

This commit is contained in:
2025-04-24 21:42:32 +08:00
parent c76dabfdb7
commit b8bb4f6b5e
11 changed files with 233 additions and 185 deletions

View File

@@ -85,11 +85,11 @@ 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>
/// <param name="bits1">第一个比特值</param>
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
/// <param name="bits2">第二个比特值</param>
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
/// <returns>合并后的二进制字节数组</returns>
public static Result<byte[]> MultiBitsToBytes(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
{
return NumberToBytes(MultiBitsToNumber(bits1, bits1Len, bits2, bits2Len).Value,
@@ -99,11 +99,11 @@ 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>
/// <param name="bits1">第一个比特值</param>
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
/// <param name="bits2">第二个比特值</param>
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
/// <returns>合并后的整型值</returns>
public static Result<ulong> MultiBitsToNumber(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
@@ -115,11 +115,11 @@ 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>
/// <param name="bits1">第一个比特值</param>
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
/// <param name="bits2">第二个比特值</param>
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
/// <returns>合并后的整型值</returns>
public static Result<uint> MultiBitsToNumber(uint bits1, uint bits1Len, uint bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
@@ -131,10 +131,10 @@ namespace Common
/// <summary>
/// 比特位检查
/// </summary>
/// <param name="srcBits">[TODO:parameter]</param>
/// <param name="dstBits">[TODO:parameter]</param>
/// <param name="mask">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="srcBits">源比特值</param>
/// <param name="dstBits">目标比特值</param>
/// <param name="mask">掩码默认为全1</param>
/// <returns>检查结果(是否匹配)</returns>
public static bool BitsCheck(ulong srcBits, ulong dstBits, ulong mask = 0xFFFF_FFFF_FFFF_FFFF)
{
return (srcBits & mask) == dstBits;
@@ -143,10 +143,10 @@ namespace Common
/// <summary>
/// 比特位检查
/// </summary>
/// <param name="srcBits">[TODO:parameter]</param>
/// <param name="dstBits">[TODO:parameter]</param>
/// <param name="mask">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="srcBits">源比特值</param>
/// <param name="dstBits">目标比特值</param>
/// <param name="mask">掩码默认为全1</param>
/// <returns>检查结果(是否匹配)</returns>
public static bool BitsCheck(uint srcBits, uint dstBits, uint mask = 0xFFFF_FFFF)
{
return (srcBits & mask) == dstBits;
@@ -156,9 +156,9 @@ namespace Common
/// <summary>
/// 字符串转二进制字节数组
/// </summary>
/// <param name="str">[TODO:parameter]</param>
/// <param name="numBase">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
/// <param name="str">输入的字符串</param>
/// <param name="numBase">进制默认为16进制</param>
/// <returns>转换后的二进制字节数组</returns>
public static byte[] StringToBytes(string str, int numBase = 16)
{
var len = str.Length;
@@ -173,10 +173,52 @@ namespace Common
return bytes;
}
/// <summary>
/// 反转字节数组中的子数组
/// </summary>
/// <param name="srcBytes">源字节数组</param>
/// <param name="distance">子数组的长度(反转的步长)</param>
/// <returns>反转后的字节数组</returns>
public static Result<byte[]> ReverseBytes(byte[] srcBytes, int distance)
{
if (distance <= 0)
return new(new ArgumentException("Distance can't be negetive", nameof(distance)));
var srcBytesLen = srcBytes.Length;
if (distance > srcBytesLen)
return new(new ArgumentException(
"Distance is larger than bytesArray", nameof(distance)));
if (srcBytesLen % distance != 0)
return new(new ArgumentException(
"The length of bytes can't be divided by 2 without reminder", nameof(distance)));
var dstBytes = new byte[srcBytesLen];
var buffer = new byte[distance];
for (int i = 0; i < srcBytesLen; i += distance)
{
var end = i + distance;
buffer = srcBytes[i..end];
Array.Reverse(buffer);
Array.Copy(buffer, 0, dstBytes, i, distance);
}
return dstBytes;
}
}
/// <summary>
/// 字符串处理工具
/// </summary>
public class String
{
/// <summary>
/// 反转字符串
/// </summary>
/// <param name="s">输入的字符串</param>
/// <returns>反转后的字符串</returns>
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();