C# 替换 Byte[] 中的字节 [英] C# Replace bytes in Byte[]

查看:72
本文介绍了C# 替换 Byte[] 中的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

替换字节数组中某些字节的最佳方法是什么??

What is the best way to replace some bytes in a byte array??

例如我有 bytesFromServer = listener.Receive(ref groupEP); 并且我可以做 BitConverter.ToString(bytesFromServer) 将其转换为可读格式以返回类似的东西

For instance i have bytesFromServer = listener.Receive(ref groupEP); and i can do BitConverter.ToString(bytesFromServer) to convert it into a readable format to return something along the lines of

48 65 6c 6c 6f 20 
74 68 65 72 65 20 
68 65 6c 70 66 75 
6c 20 70 65 6f 70 
6c 65   

我想将68 65 6c"中的某些内容替换为68 00 00"之类的内容(仅作为示例).byte[] 上没有 .Replace().

I would like to replace something inside of that making "68 65 6c" to something like "68 00 00" (just as an example). There is not .Replace() on a byte[].

是否有一种简单的方法可以将其转换回字节 []?

Would there be an easy way to convert that back into a byte[]?

任何帮助表示赞赏.谢谢!

Any help appreciated. Thank you!

推荐答案

您可以对其进行编程......一个错误我没有完全测试这个...

You could program it.... try this for a start... this is however not robust not production like code yet...beaware of off-by-one errors I didn't fully test this...

    public int FindBytes(byte[] src, byte[] find)
    {
        int index = -1;
        int matchIndex = 0;
        // handle the complete source array
        for(int i=0; i<src.Length; i++)
        {
            if(src[i] == find[matchIndex])
            {
                if (matchIndex==(find.Length-1))
                {
                    index = i - matchIndex;
                    break;
                }
                matchIndex++;
            }
            else if (src[i] == find[0])
            {
                matchIndex = 1;
            }
            else
            {
                matchIndex = 0;
            }

        }
        return index;
    }

    public byte[] ReplaceBytes(byte[] src, byte[] search, byte[] repl)
    {
        byte[] dst = null;
        int index = FindBytes(src, search);
        if (index>=0)
        {
            dst = new byte[src.Length - search.Length + repl.Length];
            // before found array
            Buffer.BlockCopy(src,0,dst,0, index);
            // repl copy
            Buffer.BlockCopy(repl,0,dst,index,repl.Length);
            // rest of src array
            Buffer.BlockCopy(
                src, 
                index+search.Length , 
                dst, 
                index+repl.Length, 
                src.Length-(index+search.Length));
        }
        return dst;
    }

作为扩展方法实现

public void Replace(this byte[] src, byte[] search, byte[] repl)
{
      ReplaceBytes(src, search, repl);
}

使用普通方法:

ReplaceBytes(bytesfromServer, 
             new byte[] {0x75, 0x83 } , 
             new byte[]{ 0x68, 0x65, 0x6c});

扩展方法使用:

bytesfromServer.Replace(
             new byte[] {0x75, 0x83 }, 
             new byte[]{ 0x68, 0x65, 0x6c});

这篇关于C# 替换 Byte[] 中的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆