converting ansi to utf8 format - is there anything wrong with it ? urgently requires help

J

James

this is a console program to convert ANSI to UTF8 format. Although in
notepad i open the source file (which is ansi), and after running the
program below, and re-open in notepad (it shows utf8 encoding), does it mean
that it has been correctly converted ?

Pls let me know what i have done wrong in conversion ...



Module Module1

Dim args As String() = Environment.GetCommandLineArgs()

Dim line As String

Const newfilename As String = "_utf8"

Sub Main()

Try

If args.Length = 2 Then

Dim sr As StreamReader = New StreamReader(args(1))

Dim SwFromFile As StreamWriter = New StreamWriter(args(1) & newfilename,
False, System.Text.Encoding.UTF8, 512)

Do

line = sr.ReadLine()

SwFromFile.Write(line)

Loop Until line Is Nothing

sr.Close()

SwFromFile.Flush()

SwFromFile.Close()

End If

Catch E As Exception

Environment.Exit(1)

Console.WriteLine("The file could not be read:")

Console.WriteLine(E.Message)

End Try

Environment.Exit(1)

End Sub

End Module
 
A

Armin Zingler

James said:
this is a console program to convert ANSI to UTF8 format. Although
in notepad i open the source file (which is ansi), and after running
the program below, and re-open in notepad (it shows utf8 encoding),
does it mean that it has been correctly converted ?

Pls let me know what i have done wrong in conversion ...



Module Module1

Dim args As String() = Environment.GetCommandLineArgs()

Dim line As String

Const newfilename As String = "_utf8"

Sub Main()

Try

If args.Length = 2 Then

Dim sr As StreamReader = New StreamReader(args(1))

I would pass System.Text.Encoding.Default as the 2nd argument to the
constructor.


Armin
 

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