How to get a null terminated string from a C# string?

前端 未结 4 886
耶瑟儿~
耶瑟儿~ 2020-12-03 13:49
  • I am communicating with a server who needs null terminated string
  • How can I do this smartly in C#?
4条回答
  •  一个人的身影
    2020-12-03 14:29

    I think the smart way is to do it simply.

    string str = "An example string" + char.MinValue; // Add null terminator.
    

    Then convert it into bytes to send to the server.

    byte[] buffer = ASCIIEncoding.ASCII.GetBytes(str);
    

    Of course what encoding you use depends on what encoding the server expects.

提交回复
热议问题