- I am communicating with a server who needs null terminated string
- How can I do this smartly in C#?
I am communicating with a server who needs null terminated string How can I
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume you’re implementing some kind of binary protocol, if the strings are null terminated. Are you using a
BinaryWriter?The default
BinaryWriterwrites strings as length prefixed. You can change that behavior:Then you can just write any string like this:
You may need to adjust the
_encodingbit, depending on your needs.You can of course expand the class with specific needs for other data types you might need to transfer, keeping your actual protocol implementation nice and clean. You’ll probably also need your own (very similar)
BinaryReader.