I have a string from UTF8 and want to convert that to Unicode (UTF16). Please help.
-
6I suggest you read this article first by Joel Splosky joelonsoftware.com/articles/Unicode.htmlsh_kamalh– sh_kamalh2011-05-11 10:16:18 +00:00Commented May 11, 2011 at 10:16
-
Hi @Fraisal, isn't UTF8 an implementation of Unicode? en.wikipedia.org/wiki/UnicodeAlex KeySmith– Alex KeySmith2011-05-11 10:16:53 +00:00Commented May 11, 2011 at 10:16
-
1UTF8 is Unicode. Do you mean that you want to convert it to UTF-16, which is teh Unicode encoding used by .NET?Stack Overflow is garbage– Stack Overflow is garbage2011-05-11 10:16:57 +00:00Commented May 11, 2011 at 10:16
-
1All strings in C# are in Unicode. Be more specific in string source and target.pabdulin– pabdulin2011-05-11 10:17:37 +00:00Commented May 11, 2011 at 10:17
-
@jalf, yes i want to convert it to utf16Faisal– Faisal2011-05-11 10:18:49 +00:00Commented May 11, 2011 at 10:18
2 Answers
If you have a file and you know that encoding of the file is UTF8 you can use StreamReader to read the file as if it is encoded in UTF8.
Regarding conversion from UTF8 to Unicode, you are comparing 2 different things. Check the link in my comment to your question.
System.Text.UTF8Encoding is UTF8 System.Text.UnicodeEncoding is UTF16. Check this link for conversion. You would be using Encoding.Convert()
Use System.Text.Encoding.UTF8.GetString().
Pass in your UTF-8 encoded text, as a byte array. The function returns a standard .net string which is encoded in UTF-16.