Does C# have something like PHP's mb_convert_encoding()?

前端 未结 3 470
遇见更好的自我
遇见更好的自我 2021-01-16 05:46

Is there a way on C# that I can convert unicode strings into ASCII + html entities, and then back again? See, in PHP, I can do it like so:



        
3条回答
  •  温柔的废话
    2021-01-16 06:38

    Yes, there's Encoding.Convert, although I rarely use it myself:

    string text = "Jöhan Strauß";
    byte[] ascii = Encoding.ASCII.GetBytes(text);
    byte[] utf8 = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, ascii);
    

    I rarely find I want to convert from one encoded form to another - it's much more common to perform a one way conversion from text to binary (Encoding.GetBytes) or vice versa (Encoding.GetString).

提交回复
热议问题