C# Replace bytes in Byte[]

后端 未结 6 915
日久生厌
日久生厌 2020-12-06 14:01

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

For instance i have bytesFromServer = listener.Receive(ref groupEP); and i can do Bit

6条回答
  •  臣服心动
    2020-12-06 14:29

    Something i pieced together... Going to test it soon. Credits from How do you convert Byte Array to Hexadecimal String, and vice versa?

         public byte[] ReplaceBytes(byte[] src, string replace, string replacewith)
        {
            string hex = BitConverter.ToString(src);
            hex = hex.Replace("-", "");
            hex = hex.Replace(replace, replacewith);
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }
    

提交回复
热议问题