IProgrammer<T>
C#, SQL, JavaScript and more
Saturday, April 21, 2007
Convert ASCII to Unicode in C#
Encoding ascii = Encoding.UTF32;
Byte[] b = ascii.GetBytes(c.ToString());
2 comments:
doesnt work!! getString accepts ubyte [] not a byte[], can someone please update this post.
Maybe this works better:
private string ConvertAsciiToUnicode(string theAsciiString) <
// Create two different encodings.
Encoding aAsciiEncoding = Encoding.ASCII;
Encoding aUnicodeEncoding = Encoding.Unicode;
// Convert the string into a byte[].
byte[] aAsciiBytes = aAsciiEncoding.GetBytes(theAsciiString);
// Perform the conversion from one encoding to the other.
byte[] aUnicodeBytes = Encoding.Convert(aAsciiEncoding, aUnicodeEncoding,
// Convert the new byte[] into a char[] and then into a string.
char[] aUnicodeChars = new
char[aUnicodeEncoding.GetCharCount(aUnicodeBytes, 0, aUnicodeBytes.Length)];
aUnicodeEncoding.GetChars(aUnicodeBytes, 0, aUnicodeBytes.Length,
string aUnicodeString = new string(aUnicodeChars);