Encoding string conversion

Asked

Viewed 1,618 times

2

I have the following string Cinto De Segurança How do I convert her to be like Cinto De Segurança

  • You’re making a web page?

  • 1

    yes I pass it in the future via Rest to the front, but I have to solve it in Beck. but I opted for rovann’s reply because they all worked but he was the first to reply thank everyone !

3 answers

5

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 prevent the character sequence from exiting in an unwanted way use:

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

So the characters come out correctly as UTF-8.

4


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 in the Dotnetfiddle

4

You can use the System.Text.Encoding

// 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 working here: https://dotnetfiddle.net/5Kw99i

Browser other questions tagged

You are not signed in. Login or sign up in order to post.