1

Hello StackOverflowers,

I read somewhere that C# string are not null terminated. M'fine ! But I'd like to know if the method :

byte[] myBytes = Encoding.ASCII.GetBytes(myString);

add a null termination character to the end of the array or if I must do it manually ? (the C system which will use this array does need it).

If manual, I guess the way would be :

byte[] myBytes = Encoding.ASCII.GetBytes(myString + '\0');

The data is meant to be ASCII, no worries about encoding.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
Kinxil
  • 306
  • 2
  • 11
  • 2
    If your question essentially boils down to "how do I correctly marshal a string to unmanaged code expecting a null-terminated string?" then there are completely different options and answers available to you. – Lasse V. Karlsen Jul 17 '18 at 13:17
  • 4
    Are you passing this to an unmanaged function via P/Invoke? Because if so, the chances are that you're doing this wrong... The NUL character will be added automatically if you declare your P/Invoke correctly. – Matthew Watson Jul 17 '18 at 13:19

4 Answers4

3

add a null termination character to the end of the array or if I must do it manually

TL;DR - it depends. You don't need it for pure .NET development. If you intend to use p-invoke to native code, well .NET does the translation for you, so again you don't.


I read somewhere that C# string are not null terminated

Correct.

MSDN:

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0'). The Length property of a string represents the number of Char objects it contains, not the number of Unicode character. - Tell me more...

OP:

But I'd like to know if the method byte[] myBytes = Encoding.ASCII.GetBytes(myString); add a null termination character to the end of the array or if I must do it manually

No it doesn't and you don't need to, unless of course you wish to pass a string to native code via p-invoke but that is a completely different question (and is handled for you anyway by .NET).

3

It doesn't add, your second code will work.

byte[] myBytes = 
         Encoding.ASCII.GetBytes("A"); // gives {65}

byte[] myBytes = 
    Encoding.ASCII.GetBytes("A"+"\0"); // gives {65, 0}
Magnetron
  • 7,495
  • 1
  • 25
  • 41
0

GetBytes does not add a null termination character. According to this SO answer, you can just add char.MinValue to the end of your string.

byte[] myBytes = Encoding.ASCII.GetBytes(myString + char.MinValue);
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
0

From MSDN

Your Unicode applications should always cast zero to TCHAR when using null-terminated strings.

So, to have a null-terminated string, we have to add a byte in the end by this code. The default value of byte is 0

byte[] bytes= Encoding.Default.GetBytes(myString);
byte[] bytesNull = new byte[bytes.Length + 1];
bytes.CopyTo(bytesNull , 0);
Antoine V
  • 6,998
  • 2
  • 11
  • 34