StreamReader Question

atmosphere1212

Active member
Joined
Aug 31, 2004
Messages
42
Hey,

Im reading in characters of text, one by one, and I want to replace any odd characters of text (above ascii 128) with spaces.

However.. when i use

someInt = myStreamReader.Read

It seems to completely skip over these odd characters.. How can I read these characters?
 
Last edited by a moderator:
HJB417 said:
try using a binary reader. basically, a class that derives from Stream


This doesnt work either. Heres my code

Code:
            Dim curChar As Integer = bStream.Read()

            While curChar <> 13 And curChar <> 26 And curChar <> -1
                curLine += Chr(stripErrorChars(curChar))
                curChar = bStream.Read
            End While

That was the code which reads a line of text. The following is the stripErrorChars method

Code:
        Private Function stripErrorChars(ByVal curChar As Integer) As Integer

            If curChar = 0 Then
                Return 32
            ElseIf curChar >= 128 Then
                Return 32
            Else
                Return curChar
            End If

        End Function
 
Last edited by a moderator:
What kind of encoding does the text file use? If the data is stored using unicode then you cannot assume each byte represents one character in that manner.

You may be better of using something like System.Text.ASCIIEncoding to convert the data to an ASCII based output instead.
 
atmosphere1212 said:
This doesnt work either

ive never seen a class that derives from system.io.stream that has a read method that takes no parameters.

youre probably using the BinaryReader class which is not derived from system.io.stream and if so, maybe thats your mistake.

The encoding will matter if its unicode16 or higher.
 
Different encodings are not just a problem with UTF-16, there are various Double Byte encodings (e.g. Shift_JIS), UTF-8 is a Multi-Byte encoding format as well.
 
Back
Top