How to get a null terminated string from a C# string?
问题 I am communicating with a server who needs null terminated string How can I do this smartly in C#? 回答1: I assume you're implementing some kind of binary protocol, if the strings are null terminated. Are you using a BinaryWriter ? The default BinaryWriter writes strings as length prefixed. You can change that behavior: class MyBinaryWriter : BinaryWriter { private Encoding _encoding = Encoding.Default; public override void Write(string value) { byte[] buffer = _encoding.GetBytes(value); Write