Encoding

Unicode and .Net

http://www.yoda.arachsys.com/csharp/unicode.html

Get encoded string from byte[]

char[] chars = new char[Encoding.Unicode.GetCharCount(bytes, 0, bytes.Length)];

Write string to disk as Unicode

File.WriteAllText(@"c:\regtest.xml", new string (chars), UnicodeEncoding.Unicode);

Encoding and decoding byte arrays as strings

This example uses UTF8 encoding.

string text = "unicode string";
byte[] bytes = Encoding.UTF8.GetBytes(text);
string byteText = BitConverter.ToString(bytes));

Encode Base64 string

string        base64Text    = Convert.ToBase64String(bytes);
 
    UTF8Encoding    encoder     = new System.Text.UTF8Encoding();  
    Decoder     utf8Decode     = encoder.GetDecoder();

Decode Base64 string

byte[]         bytes        = Convert.FromBase64String(string);
    int         charCount    = utf8Decode.GetCharCount(bytes, 0, bytes.Length);    
    char[]         chars        = new char[charCount];
    utf8Decode.GetChars(todecode_byte, 0, bytes.Length, chars, 0);
 
    return new String(chars);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License