Is it possible to display (convert?) the unicode hex \u0092 to an unicode html entity in .NET?

后端 未结 2 713
情话喂你
情话喂你 2021-01-19 07:22

I have some string that contains the following code/value:

\"You won\\u0092t find a ....\"

It looks like that string contains the Right

相关标签:
2条回答
  • 2021-01-19 08:01

    It looks like there's an encoding mix-up. In .NET, strings are normally encoded as UTF-16, and a right apostrophe should be represented as \u2019. But in your example, the right apostrophe is represented as \x92, which suggests the original encoding was Windows code page 1252. If you include your string in a Unicode document, the character \x92 won't be interpreted properly.

    You can fix the problem by re-encoding your string as UTF-16. To do so, treat the string as an array of bytes, and then convert the bytes back to Unicode using the 1252 code page:

    string title = "You won\u0092t find a cheaper apartment * Sauna & Spa";
    byte[] bytes = title.Select(c => (byte)c).ToArray();
    title = Encoding.GetEncoding(1252).GetString(bytes);
    // Result: "You won’t find a cheaper apartment * Sauna & Spa"
    
    0 讨论(0)
  • 2021-01-19 08:02

    Note: much of my answer is based on guessing and looking at the decompiled code of System.Web 4.0. The reference source looks very similar (identical?).

    You're correct that "’" (6 characters) can be displayed in the browser. Your output string, however, contains "\u0092" (1 character). This is a control character, not an HTML entity.

    According to the reference code, WebUtility.HtmlEncode() doesn't transform characters between 128 and 160 - all characters in this range are control characters (ampersand is special-cased in the code as are a few other special HTML symbols).

    My guess is that because these are control characters, they're output without transformation because transforming it would change the meaning of the string. (I tried running some examples using LinqPad, this character was not rendered.)

    If you really want to transform these characters (or remove them), you'll probably have to write your own function before/after calling HtmlEncode() - there may be something that does this already but I don't know of any.

    Hope this helps.

    Edit: Michael Liu's answer seems correct. I'm leaving my answer here because it may be useful in cases when the input encoding of a string is not known.

    0 讨论(0)
提交回复
热议问题