Question Encoding from one encoded format to another

Anand Mani Tiwari

New member
Joined
Jun 15, 2011
Messages
4
Programming Experience
1-3
Hi,

I have a challange to convert the detected Encoding format of Text file to selected Encoding format. Like if the detected file is in Utf-8 and if user want to convert the encoding to Utf-16 or ANCII or Unicode. It will happen on one button click and the file will convert to the desired output encoding format.

Right Now I am able to detect the encoding but conversion not happening. Please help me out ASAP...

Thanking you for this help.
 
You simply call File.ReadAllText and specify the current encoding to read the data into a String and then call File.WriteAllText and specify the new encoding to write the String back to the file.
 
Thanks for the quick reply, I had did like below mentioned code:

Using sr AsTextReader = NewStreamReader(fs, Encoding.GetEncoding("ISO-8859-1"))
Using output AsTextWriter = NewStreamWriter(NewFileStream(destPath, FileMode.Create), encodingType)
While (sr.Peek() >= 0)
output.Write(sr.ReadLine())
EndWhile
EndUsing
End Using


But the problem is when Iam opening the Text file in Notepad++ it is not showing the Symbol like 'CR LF' at the end of line. Please help me on this...

Thanking you for this.
 
output.Write(output.Write(sr.ReadLine())

it is not showing the Symbol like 'CR LF' at the end of line.
ReadLine will return a NewLine terminated line excluding terminators.
WriteLine will append NewLine to the written text.

jmcilhinneys suggestion is a lot easier though, and would be better to do unless your files are really large.
 
Thank you, It was really helpful, but when I am converting it any formats of encoding is showing all encoding symbol as CR LF, how I will differentiate them as LF for unicode and all....

Thank you again.
 
While you can set NewLine terminator for the StreamWriter, doing as suggested eliminates that and preserves current terminators in file.
 
What's to elaborate? You're trying complicate something that's very simple. Call File.ReadAllText. It returns a String. Call File.WriteAllText and pass that String. That's all there is to it.
 
Back
Top