Convert a string to or from another encoding

suggest change

.NET strings contain System.Char values (UTF-16 code units). To save or manage text in another encoding, work with a byte array.

System.Text.Encoding and its derived classes convert between those bytes and a UTF-16 string. Common encodings include UTF-8 and UTF-16. Encoder and Decoder are the lower-level types used when a conversion is split across several buffers.

Examples:

Convert a string to UTF-8

byte[] data = Encoding.UTF8.GetBytes("This is my text");

Convert UTF-8 data to a string

var text = Encoding.UTF8.GetString(data);

Change encoding of an existing text file

This code reads a UTF-8 text file and writes it back as UTF-16. It is not suitable for a large file, because it loads the whole file into memory:

var content = File.ReadAllText(path, Encoding.UTF8);
File.WriteAllText(path, content, Encoding.UTF16);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents