开发者

Encoding of Pound symbol in ASP

I'm pulling text from an external system (SSAS) and displaying the text in a TableCell in ASP 2.0 (.NET 4). The text is being displayed as £14,243,123.26 (note the  in front of the pound symbol).

My understanding of this is that c# uses UTF-16 (SSAS probably the same since SQL Server does also), and ASP 2.0 will display the pages as UTF-8 which is what is happening. The difference here is probably causing the display issue.

However this issue seems to be generally han开发者_JS百科dled under the hood so I'm wondering why not here? Is it because many controls in ASP automatically encode the strings where as the Table control doesn't? Using Server.HtmlEncode on the Table seems to work but I'm hoping this isn't necessary everywhere.


The UTF-8/UTF-16 conversion shouldn't be a problem. You need to trace exactly where things are going wrong, with diagnostics and with tools:

  • Use Wireshark to look at the exact response coming back. You need to check the content type, any charset declarations within the page itself, and the bytes used to represent the £ sign
  • At the server, when you've fetched the data, dump the exact characters involved, something like this (assuming some appropriate Log.Write method):

    Log.Write("Retrieved string: '{0}' length {1}", text, text.Length);
    foreach (char c in text)
    {
        Log.Write("  Character {0}: U+{1:x4}", c, (int) c);
    }
    
  • If you are able to talk to the server from a console app, try that instead, dumping the characters out as per the above. This is likely to make experimentation much simpler than messing around with redeployment and server logs.

Once you've worked out exactly where the issue is, you can edit your question to give that information if you can't work out how to fix it.


The most likely cause is that you aren't setting the encoding for the ASP.NET page to UTF-8.

You can check whether this is the issue by forcing the page encoding to UTF-8 within your web browser. If that fixes the £ symbol display then the problem is with your page encoding setting.

The fact that Server.HtmlEncode fixes the problem is a good pointer to it being the page encoding that is a fault too.

The problem isn't a mismatch between UTF-16 and UTF-8 within ASP.NET and c#. Windows and c# use a form of UTF-16 internally, but that doesn't stop ASP.NET from emitting UTF-8 encoded pages when asked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜