I need to read a binary file which has mixed data types embedded in it.
Fixed length strings, 16-bit integers and zero (null) terminated ASCII
strings. (akak ASCIIZ)
The first two types are no problem, but the zero terminated string is a
problem because I have to read byte--by-byte with BinaryStream(), and I
don't know whether the class or the O/S uses buffering to make this
efficient. Is my code slowing the app down - is there a better class or
technique other than reading in byte-by-byte and checking for char 0?
(Code below)
Thanks,
Carlo
/// <summary>
/// Read an AsciiZ string in from the binary reader
/// </summary>
/// <param name="dicReader">Binary reader instance</param>
/// <returns>String, null terminator is truncted,
/// stream reader positioned at byte after null</returns>
public static string ReadStringZ(BinaryReader reader) {
string result = "";
char c;
for (int i = 0; i < reader.BaseStream.Length; i++) {
if ((c = (char) reader.ReadByte()) == 0) {
break;
}
result += c.ToString();
}
return result;
}