How to write a utf8 string in a unicode text file using c#?
-
Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();
-
Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();
Maybe this can help you - http://support.microsoft.com/kb/816149.
My signature "sucks" today
-
Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();
it is all in the documentation[^], as usual. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).