.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Encoding problem
 

Encoding problem

How can I convert a string into unicode using c#.net?

sandiliya
Hello
you can try the following

string unicodeString = "This string contains the unicode character Pi(\u03a0)";

// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);


Amolpbhavsar

Use following code

Dim bytes() As Byte = Encoding.Default.GetBytes("String Data")
Dim unicodeBytes() As Byte = Encoding.Convert(Encoding.Default, Encoding.Unicode, bytes)
Dim unicodeString As String = Encoding.Unicode.GetString(unicodeBytes)

Gaurav Khanna
Khanna Gaurav
All C# strings are already Unicode. Try typing more than a half a line when you ask a question.

Hans Passant.
nobugz

You can use google to search for other answers

Custom Search

More Threads

• bitmap image difference
• XLS sheet data
• Class diagram
• system.invalidcastexception
• Access Function in other project of same solution....
• Separate bytedata
• EventLog errors due to events being written whilst I'm reading them
• Name and type of calling child class
• Accessing Web.Config from legacy VB6 and ASP
• Using webbrowser control (.NET 2.0)