Conversion of encoding string

1

I have the following string Cinto De Segurança How do I convert her to stay as Cinto De Segurança

    
asked by anonymous 03.04.2018 / 19:56

3 answers

3

Convert to bytes, and then to string again using another encoding:

string x = "Cinto De Segurança";
byte[] bytes = Encoding.Default.GetBytes(x);
string y = Encoding.UTF8.GetString(bytes);
Console.WriteLine(y);

I put it in DotNetFiddle

    
03.04.2018 / 20:00
4

You can do this in a very simple way:

Encoding.UTF8.GetString(Encoding.Default.GetBytes("Cinto De Segurança"));

Use the namespace: System.Text .

using System.Text;

According to this answer in SOen , if you are using the console, to avoid that the string does not exit from an unwanted form use:

Console.OutputEncoding = System.Text.Encoding.UTF8;

For the characters to output correctly as UTF-8 .

    
03.04.2018 / 20:01
3

You can use the System.Text.Encoding classes

// Obtém um array de bytes do texto
byte[] bytes = System.Text.Encoding.Default.GetBytes("Cinto De Segurança");
// Codifica o obtém a string. Usando UTF8, mas pode usar outros, como ASCII ou Unicode 
string retorno = System.Text.Encoding.UTF8.GetString(bytes);

See it working here: link

    
03.04.2018 / 20:05