Peter
Peter

Reputation: 7804

ascii character not showing in browser

I have an MVC Razor view

@{
ViewBag.Title = "Index";
var c = (char)146;
var c2 = (short)'’';
}
<h2>@c --- @c2 --’-- ‘Why Oh Why’ & &#146;</h2> 
@String.Format("hi {0} there", (char)146)

characters stored in my database in varchar fields are not rendering to the browser. This example demonstrates how character 146 doesn't show up How do I make them render?

[EDIT]

When I do this the character 146 get converted to UNICODE 8217 but if 146 is attempted to be rendered directly on the browser it fails

    public ActionResult Index()
    {

        using (var context = new DataContext())
        {
            var uuuuuggghhh = (from r in context.Projects
                               where r.bizId == "D11C6FD5-D084-43F0-A1EB-76FEED24A28F"
                              select r).FirstOrDefault();

            if (uuuuuggghhh != null)
            {
                var ca = uuuuuggghhh.projectSummaryTxt.ToCharArray();
                ViewData.Model = ca[72]; // this is the character in question
                return View();
            }
        }


        return View();
    }

Upvotes: 1

Views: 2491

Answers (3)

Peter
Peter

Reputation: 7804

We finally agreed that the data was corrupt we have asked users who can't see this character rendered to fix the source data

Upvotes: 0

bobince
bobince

Reputation: 536329

You do not want to use character 146. Character 146 is U+0092 PRIVATE USE TWO, an obscure and useless control character that typically renders as invisible, or a missing-glyph box/question mark.

If you want the character : that is U+2019 SINGLE RIGHT QUOTATION MARK, which may be written directly or using &#x2019; or &#8217;.

146 is the byte number of the encoding of U+2019 into the Windows Western code page (cp1252), but it is not the Unicode character number. The bottom 256 Unicode characters are ordered the same as the bytes in the ISO-8859-1 encoding; ISO-8859-1 is similar to cp1252 but not the same.

Bytes 128–159 in cp1252 encode various typographical niceties like smart quotes, whereas bytes 128–159 in ISO-8859-1 (and hence characters 128–159 in Unicode) are seldom-used control characters. For web applications, you usually want to filter out the control characters (0–31 and 128–159 amongst a few others) as they come in, so they never get as far as the database.

If you are getting character 146 out of your database where you expect to have a smart quote, then you have corrupt data and you need to fix it up before continuing, or possibly you are reading the database using the wrong encoding (quite how this works depends what database you're talking to).

Now here's the trap. If you write:

&#146;

as a character reference, the browser actually displays the smart quote U+2019 , and, confusingly, not the useless control character that actually owns that code point!

This is an old browser quirk: character references in the range &#128; to &#159; are converted to the character that maps to that number in cp1252, instead of the real character with that number.

This was arguably a bug, but the earliest browsers did it back before they grokked Unicode properly, and everyone else was forced to follow suit to avoid breaking pages. HTML5 now documents and sanctions this. (Though not in the XHTML serialisation; browsers in XHTML parsing mode won't do this because it's against the basic rules of XML.)

Upvotes: 1

GONeale
GONeale

Reputation: 26494

@Html.Raw(((char)146).ToString())

or

@Html.Raw(String.Format("hi {0} there", (char)146))

both appear to work. I was testing this in Chrome and kept getting blank data, after viewing with FF I can confirm the representation was printing (however 146 doesn't appear to be a readable character).

This is confirmed with a readable character '¶' below:

@Html.Raw(((char)182).ToString())

Not sure why you would want this though. But best of luck!

Upvotes: 1

Related Questions