Reading a zero-terminated string

I am reading lines from a binary file. Each line has zero termination. Encoding - UTF-8. In python, I just read the byte, check if it is 0, add it to the byte array and continue reading bytes until I see 0. Then I convert the byte array to a string and move on. All lines were read correctly.

How can I read this in C #? I don’t think I have the luxury of just adding bytes to an array, since arrays are of a fixed size.

+5
source share
3 answers

You can use List<byte>:

List<byte> list = new List<byte>();
while(reading){ //or whatever your condition is
    list.add(readByte);
}

string output = Encoding.UTF8.GetString(list.ToArray());

Or you can use StringBuilder:

StringBuilder builder = new StringBuilder();

while(reading){
    builder.Append(readByte);
}

string output = builder.ToString();
+4
source

The following should get what you are looking for. All text should be inside the myText list.

var data = File.ReadAllBytes("myfile.bin");
List<string> myText = new List<string>();
int lastOffset = 0;
for (int i = 0; i < data.Length; i++)
{
    if (data[i] == 0)
    {
        myText.Add(System.Text.Encoding.UTF8.GetString(data, lastOffset, i - lastOffset));
        lastOffset = i + 1;
    }
}
+6

, StreamReader:

StringBuilder sb = new StringBuilder();
using(StreamReader rdr = OpenReader(...)) {
    Int32 nc;
    while((nc = rdr.Read()) != -1) {
          Char c = (Char)nc;
          if( c != '\0' ) sb.Append( c );
    }
}
+3
source

All Articles