Reading a null-terminated string

前端 未结 4 772
醉梦人生
醉梦人生 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:00

    I assume you're using a StreamReader instance:

    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 );
        }
    }
    

提交回复
热议问题