Encoding et CSharp

De Banane Atomic
Aller à la navigationAller à la recherche

Généralités

Csharp.svg
byte[] bytes = Encoding.Default.GetBytes("é");  // bytes[0] → 233 (10)
bytes[0].ToString("X");  // E9 (16)

string utf8String = Encoding.UTF8.GetString(bytes);  // é
string s1 = Char.ConvertFromUtf32(bytes[0]);         // é
string s2 = Char.ConvertFromUtf32(0xE9);             // é
Les string en mémoire sont encodé en UTF-16.
Encoding.Default the operating system's current ANSI code page
Encoding.UTF8 UTF-8 avec BOM
Encoding.Unicode UTF-16 format using the little endian byte order
Encoding.BigEndianUnicode UTF-16 format using the big endian byte order
Encoding.UTF32 UTF-32 format using the little endian byte order

Changer la base d'un chiffre

UTF-8 sans BOM

Csharp.svg
Encoding utf8WithBom = new UTF8Encoding();

Encoding utf8WithoutBom = new UTF8Encoding(false);

using (TextWriter tw = new StreamWriter(filePath, false, utf8WithoutBom))
{
    tw.WriteLine("text");
}
StreamWriter pour écrire dans un flux (file, buffer, memory)
StringWriter pour écrire dans un String

StringWriter en UTF-8

Csharp.svg
public class Utf8StringWriter : StringWriter
{
    public override Encoding Encoding
    {
        // UTF8Encoding(false) instead of Encoding.UTF8 to get utf-8 without BOM
        get { return new UTF8Encoding(false); }
    }
}

using (var sw = new Utf8StringWriter())
{
    var serializer = new XmlSerializer(monObjet.GetType());
    serializer.Serialize(sw, monObjet, null);
    string output = sw.ToString();
}
Par défaut, StringWriter utilise UTF-16.

Écrire dans un fichier

Csharp.svg
File.WriteAllText(filePath, "text"); // UTF-8 sans BOM

File.WriteAllText(filePath, "text", Encoding.UTF8); // UTF-8 avec BOM

Unicode vers ASCII

Csharp.svg
var unicodeText = "éà";
var normalizedString = unicodeText.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();

foreach (var c in normalizedString)
{
    var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
    if (unicodeCategory != UnicodeCategory.NonSpacingMark)
    {
        stringBuilder.Append(c);
    }
}

var v = stringBuilder.ToString().Normalize(NormalizationForm.FormC);

NormalizationForm