Reading a null-terminated string

前端 未结 4 760
醉梦人生
醉梦人生 2021-01-12 12:34

I am reading strings from a binary file. Each string is null-terminated. Encoding is UTF-8. In python I simply read a byte, check if it\'s 0, append it to a byte array, and

4条回答
  •  难免孤独
    2021-01-12 13:05

    Following should get you what you are looking for. All of text should be inside myText list.

    var data = File.ReadAllBytes("myfile.bin");
    List myText = new List();
    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;
        }
    }
    

提交回复
热议问题