Files
KJD_WebService/AllBase/BaseWork.cs
T
2025-06-29 19:38:31 +08:00

96 lines
2.9 KiB
C#

/*
* 基本处理
* 肖俊
* 2015-4-2
*/
using System.Text;
namespace AllBase
{
public static class BaseWork
{
/// <summary>
/// 两个数组是否相同
/// </summary>
/// <param name="b1"></param>
/// <param name="b2"></param>
/// <returns></returns>
public static bool IsSame(ref byte[] b1, ref byte[] b2)
{
if (b1.Length != b2.Length)
return false;
for (int i = 0; i < b1.Length; i++)
if (b1[i] != b2[i])
return false;
return true;
}
/// <summary>
/// 取得一个数组在另一个数组中的位置
/// </summary>
/// <param name="b1">主数组</param>
/// <param name="b2">查找数组</param>
/// <param name="start">起点</param>
/// <returns></returns>
public static int GetIndexFirst(ref byte[] b1, ref byte[] b2, int start)
{
if (b1.Length < b2.Length || start > b1.Length - 1)
return -1;
int j;
for (int i = start; i < b1.Length - b2.Length+1; i++)
{
if (b1[i] != b2[0])
continue;
for (j = 1; j < b2.Length; j++)
if (b1[i + j] != b2[j])
break;
if (j == b2.Length)
return i;
}
return -1;
}
/// <summary>
/// 一个数组是否在另一个数组的结尾
/// </summary>
/// <param name="getByte">主数据</param>
/// <param name="end">结尾数组</param>
/// <returns></returns>
public static bool IsEndByte(ref byte[] getByte, ref byte[] end)
{
if (end.Length > getByte.Length)
return false;
int i, c = getByte.Length - end.Length;
for (i = 0; i < end.Length; i++)
if (end[i] != getByte[c + i])
break;
if (i == end.Length)
return true;
return false;
}
/// <summary>
/// 取得一个数组在另一个数组中的位置
/// </summary>
/// <param name="b1">主数组</param>
/// <param name="b2">查找数组</param>
/// <returns></returns>
public static int GetIndexLast(ref byte[] b1, ref byte[] b2)
{
if (b1.Length < b2.Length)
return -1;
int j, end = b2.Length - 1;
for (int i = b1.Length - 1; i >= end; i--)
{
if (b1[i] != b2[end])
continue;
for (j = 1; j < b2.Length; j++)
if (b1[i - j] != b2[end - j])
break;
if (j == b2.Length)
return i - end;
}
return -1;
}
}
}