- I am communicating with a server who needs null terminated string
- How can I do this smartly in C#?
4 Answers
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.

- 6,881
- 3
- 29
- 30
-
8char.MinValue is the real C# way to go – A. M. Jun 18 '10 at 23:20
-
2This is by far the simplest (and most reliable) way I've found yet. This should be the accepted answer. – jhmckimm Oct 08 '16 at 01:53
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(buffer);
Write((byte)0);
}
}
Then you can just write any string like this:
using (MyBinaryWriter writer = new MyBinaryWriter(myStream))
{
writer.Write("Some string");
}
You may need to adjust the _encoding
bit, 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
.
-
2I would suggest to use base.Write(this._encoding.GetBytes(new char[]{ '\u0000'})) instead, to encode the null-terminator. The default UTF-16 is 2 bytes and expects two 0-bytes at the end. – toong Jun 18 '12 at 15:34
-
Append value with "\0" (if it doesn't end with it already): `if (!value.EndsWith("\0")) value += "\0";` therefore making the solution **encoding-independent** (you don't have to worry about the number of zeros). – marchewek Dec 18 '12 at 19:21
The strings are already null terminated. Although the string itself doesn't contain a null character, a null character always follows the string in memory.
However, strings in .NET are unicode, so they are stored as UTF-16/UCS-2 in memory, and the server might expect a different encoding, usually an 8 bit encoding. Then you would have to encode the string into a byte array and place a zero byte at the end:
byte[] data = Encoding.Default.GetBytes(theString);
byte[] zdata = new byte[data.Length + 1];
data.CopyTo(zdata, 0);
(The zdata array is all filled with zeroes when creates, so you don't have to actually set the extra byte to zero.)
-
Fixed a minor typo. I'm not too happy with the double buffer allocation personally. You can get around that. Then again, the strings are not likely to be huge or very high volume. – Thorarin May 08 '10 at 10:17
-
7"Null terminated" usually means "ending at the first null". .NET strings cannot be considered to be null-terminated if you also allow that a .NET string may contain one or more null characters and yet not terminate – John Saunders May 08 '10 at 14:10
-
"The strings are already null terminated. Although the string itself doesn't contain a null character, a null character always follows the string in memory." I've never heard this before, and didn't see any information about this on the MSDN docs. Could you post a source? – Juliet May 08 '10 at 14:16
-
1@John: What I meant specifically with the first sentence is that there is already a terminating zero after the string. If the string itself contains a null character it would naturally not work properly as a null terminated string, but that is a problem that every single answer presented here shares. – Guffa May 08 '10 at 14:19
-
4@Guffa: Jon's page seems to conflict directly with the [actual documentation](http://msdn.microsoft.com/en-us/library/ms228362.aspx). I don't think that C# strings are really null-terminated, the marshaling process just knows how to null-terminate them (and I'm not sure how it would preserve embedded nulls, or if that's even possible). – Aaronaught May 08 '10 at 14:45
-
@Aaronaught: The documentation only says that the string doesn't use a terminating null character, it doesn't say anything about whether a null character is placed after the string in the character array or not. – Guffa May 08 '10 at 15:07
-
@Juliet https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code says: A `char*` value produced by fixing a string instance always points to a null-terminated string. Within a fixed statement that obtains a pointer p to a string instance `s`, the pointer values ranging from `p` to `p + s.Length ‑ 1` represent addresses of the characters in the string, and the pointer value `p + s.Length` always points to a null character (the character with value `'\0'`). – T S Mar 08 '22 at 00:31
-
@Juliet If that is already true before a fixed statement, I don't know, although I would guess it is to improve performance of the fixed statement... – T S Mar 08 '22 at 00:33
You add a null character to the end of the string. .NET strings can contain null characters.

- 332,285
- 67
- 532
- 628
-
1I think he's suggesting 'char c = new char()', which will create the unicode point 'U+0000'. – Steve Cooper Sep 15 '10 at 15:04