Saturday, April 21, 2007

Convert ASCII to Unicode in C#

private string Convert(int asciiCode)
    Encoding ascii = Encoding.UTF32;
     char c = (char)asciiCode;
     Byte[] b = ascii.GetBytes(c.ToString());

    return ascii.GetString((b));

kick it on


Anonymous said...

doesnt work!! getString accepts ubyte [] not a byte[], can someone please update this post.

Anonymous said...

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,
aUnicodeChars, 0);
string aUnicodeString = new string(aUnicodeChars);
return aUnicodeString;