En C# String/Encodage des Caractères quelle est la différence entre GetBytes(), GetString() et la fonction Convert()?

Nous rencontrez des difficultés à obtenir une chaîne Unicode pour convertir une chaîne UTF-8 à envoyer sur le fil:

//Start with our unicode string.
string unicode = "Convert: \u10A0";

//Get an array of bytes representing the unicode string, two for each character.
byte[] source = Encoding.Unicode.GetBytes(unicode);

//Convert the Unicode bytes to UTF-8 representation.
byte[] converted = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, source);

//Now that we have converted the bytes, save them to a new string.
string utf8 = Encoding.UTF8.GetString(converted);

//Send the converted string using a Microsoft function.
MicrosoftFunc(utf8);

Bien que nous avons converti la chaîne en UTF-8, c'est de ne pas arriver en UTF-8.

OriginalL'auteur |