public static int Replace(ref Byte[] all, Byte[] s, Byte[] t)
{
if (s.Length > all.Length) return 0;
int replace_count = 0;
List<byte> temp = new List<byte>();
for (int i = 0; i < all.Length - s.Length; i++)
{
bool catch_s = true;
for (int j = 0; j < s.Length; j++)
{
if (all[i + j] != s[j])
{
catch_s = false;
break;
}
}
if (catch_s)
{
replace_count++;
temp.AddRange(t);
i += s.Length - 1;
}
else
{
temp.Add(all[i]);
}
}
temp.Add(all[all.Length - 3]);
temp.Add(all[all.Length - 2]);
temp.Add(all[all.Length - 1]);
all = temp.ToArray();
return replace_count;
}
{
if (s.Length > all.Length) return 0;
int replace_count = 0;
List<byte> temp = new List<byte>();
for (int i = 0; i < all.Length - s.Length; i++)
{
bool catch_s = true;
for (int j = 0; j < s.Length; j++)
{
if (all[i + j] != s[j])
{
catch_s = false;
break;
}
}
if (catch_s)
{
replace_count++;
temp.AddRange(t);
i += s.Length - 1;
}
else
{
temp.Add(all[i]);
}
}
temp.Add(all[all.Length - 3]);
temp.Add(all[all.Length - 2]);
temp.Add(all[all.Length - 1]);
all = temp.ToArray();
return replace_count;
}
Byte[] s = { 0x01, 0x3A, 0x01 };
Byte[] t = { 0x99 };
string t1 = System.BitConverter.ToString(ALL);
string t2 = System.BitConverter.ToString(s);
string t3 = System.BitConverter.ToString(t);
string[] arr = t1.Replace(t2, t3).Split('-');
ALL = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
ALL[i] = Convert.ToByte(arr[i], 16);
}
using System.Collections.Generic;
using System.Data;
class A
{
static void Main()
{
Byte[] ALL={ 0x0D, 0x01, 0x3A, 0x01, 0x34, 0x51, 0x70, 0x02, 0x1B, 0x0A, 0x22, 0x20, 0x0F, 0x4D };
Byte[] s={0x01,0x3A,0x01};
Byte[] t = { 0x99 };
int sub = 0;
for (int i = 0; i < ALL.Length;i++ )
{
foreach (byte bs in s)
{
if (Compare(BitConverter.GetBytes((int)ALL[i]), BitConverter.GetBytes((int)bs)))
{
ALL[sub] = t[0];
break;
}
}
}
}
static bool Compare(byte[] s1, byte[] s2)
{
if(s1.Length!=s2.Length )
return false;
for (int i = 0; i < s1.Length; i++)
{
if (s1[i] != s2[i])
return false;
else
continue;
}
return true;
}
}
{
if (s.Length > all.Length) return 0;
int replace_count = 0;
List<byte> temp = new List<byte>();
for (int i = 0; i < all.Length - s.Length + 1; i++)
{
bool catch_s = true;
for (int j = 0; j < s.Length; j++)
{
if (all[i + j] != s[j])
{
catch_s = false;
break;
}
}
if (catch_s)
{
replace_count++;
temp.AddRange(t);
i += s.Length - 1;
}
else
{
temp.Add(all[i]);
}
if (i == all.Length - s.Length)
{
if (!catch_s)
{
temp.Add(all[all.Length - 2]);
temp.Add(all[all.Length - 1]);
}
}
} all = temp.ToArray();
return replace_count;
}
BM算法不懂,搜索一下去。
using System;
using System.Collections.Generic;
using System.Data;
class A
{
static void Main()
{
Byte[] ALL = { 0x0D, 0x01, 0x3A, 0x01, 0x34, 0x51, 0x70, 0x02, 0x1B, 0x0A, 0x22, 0x20, 0x0F, 0x4D };
Byte[] s = { 0x01, 0x3A, 0x01 };
Byte[] t = { 0x99 }; string temp = "";
string target="";
string replaceStr = t[0].ToString();
int i = 0;
for ( i = 0; i < ALL.Length;i++)
{
if (i != ALL.Length - 1)
temp += ALL[i].ToString() + ",";
else
temp += ALL[i].ToString();
} for ( i = 0; i < s.Length; i++)
{
if (i != s.Length - 1)
target += s[i].ToString() + ",";
else
target += s[i].ToString();
} temp=temp.Replace(target,replaceStr); // 利用 Replace实现替换
string[] result = temp.Split(',');
ALL=new byte[result.Length]; for( i=0;i<result.Length ;i++ )
{
ALL[i]= (byte)Convert.ToInt32(result[i],16);
} }
}