Reading a null-terminated string

前端 未结 4 736
醉梦人生
醉梦人生 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 12:59

    You can either use a List:

    List list = new List();
    while(reading){ //or whatever your condition is
        list.add(readByte);
    }
    
    string output = Encoding.UTF8.GetString(list.ToArray());
    

    Or you could use a StringBuilder :

    StringBuilder builder = new StringBuilder();
    
    while(reading){
        builder.Append(readByte);
    }
    
    string output = builder.ToString();
    

提交回复
热议问题