Null Terminated ASCII String from a File Stream.

M

Michel Racicot

I need to read a null terminated ASCII string from a FileStream. How can I
do that? I know that with FileStream.ReadByte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a
way to do this easier?
 
N

Nicholas Paldino [.NET/C# MVP]

Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class (found
in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.
 
M

Michel Racicot

Kind of, thank you for the Info... But the file is binary (but includes null
terminated strings at certain offsets).

So far, I use the BinaryReader class to find the offsets of the strings, I
wanted to know if I could also use BinaryReader to read the strings
themselves.

Any idea?

Nicholas Paldino said:
Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class (found
in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michel Racicot said:
I need to read a null terminated ASCII string from a FileStream. How can I
do that? I know that with FileStream.ReadByte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a
way to do this easier?
 
G

Guest

Michel Racicot said:
Kind of, thank you for the Info... But the file is binary (but includes null
terminated strings at certain offsets).

So far, I use the BinaryReader class to find the offsets of the strings, I
wanted to know if I could also use BinaryReader to read the strings
themselves.

you use the BinaryReader to read in a byte[], then pass that to the
appropriate System.Text.Encoding class to convert to a string.
Any idea?

Nicholas Paldino said:
Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class (found
in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michel Racicot said:
I need to read a null terminated ASCII string from a FileStream. How can I
do that? I know that with FileStream.ReadByte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a
way to do this easier?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top