BYTE[]如何高效查找替换?
Byte[] ALL={ 0x0D, 0x01, 0x3A, 0x01, 0x34, 0x51, 0x70, 0x02, 0x1B, 0x0A, 0x22, 0x20, 0x0F, 0x4D }Byte[] s={0x01,0x3A,0x01}
Byte[] t={0x99}
如果s在ALL中找到直接替换为t,替换后
BYTE[] ALL={ 0x0D, 0x99,0x34, 0x51, 0x70, 0x02, 0x1B, 0x0A, 0x22, 0x20, 0x0F, 0x4D }
public int i Replace(Ref Byte[] all,Byte[] s,Byte[] t)
{
}
--------------------编程问答--------------------
public static int Replace(ref Byte[] all, Byte[] s, Byte[] t)--------------------编程问答-------------------- 使用LINQ操作byte实现替换
{
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;
}
--------------------编程问答--------------------
+1
逍遥 你不是换头像了吗?难道那天是我看错了? --------------------编程问答-------------------- 感觉那个漫画版的李逍遥太帅了点。有点娘。就换回来了。 --------------------编程问答-------------------- 还是类似BM的算法比较快啊! --------------------编程问答--------------------
哦 晓得了 --------------------编程问答-------------------- 学习ing! --------------------编程问答--------------------
Byte[] ALL = { 0x0D, 0x01, 0x3A, 0x01, 0x34, 0x51, 0x70, 0x02, 0x1B, 0x0A, 0x22, 0x20, 0x0F, 0x4D };--------------------编程问答-------------------- using System;
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;
}
}
--------------------编程问答-------------------- 晕倒 发现错了 原来是 找到3个连续的就替换 --------------------编程问答-------------------- 1楼方法少考虑一种,末尾3个也是可匹配的情况。修改一下。
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 + 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);
}
}
} --------------------编程问答-------------------- BM算法对查找字符确实很高效 --------------------编程问答-------------------- 学习ing --------------------编程问答-------------------- 学习········
补充:.NET技术 , C#