Click here to Skip to main content
Click here to Skip to main content

Full HTML Character Encoding in C#

, 28 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article shows how to take a String object and encode it as HTML using Unicode character entities for extended characters.


This is my first CodeProject article, a short and sweet answer to a problem I couldn't find the solution to anywhere else.


It is not terribly difficult to find a way to take a string containing HTML and "unescape" it to its proper characters (in fact HttpUtility.HtmlDecode() works perfectly). However, it's apparently another matter to take a complex string and escape it for HTML. At least, HttpUtility.HtmlEncode() doesn't fit the bill, as it only encodes the bare minimum of characters, ignoring such common things as curly quotes.

That said, of course, there's probably a better solution out there, so please direct me to it if you know of it, and I'll kindly remove or update this article accordingly.

Using the Code

The logic is quite simple. Basically, HTTP supports only standard ASCII characters for transmission, and extended characters are represented by named or numbered entity references. For example, the common ampersand (&) symbol is "escaped" as &. You can also use numbered entities, for example ® results in the registered (®) symbol.

Anyhow, the idea is to figure out what these numerical values are and manually encode extended characters (characters beyond 127, which are not standard ASCII) using the numbered entity format above.

The trick (hack) I discovered is simply to take a char and pass it to Convert.ToInt32(), which results in an apparently Unicode* value, which we can then convert to a string and wrap with the &# and ; characters.

* See Points of Interest below.

Here is the resulting method:

public static string HtmlEncode( string text ) {
    char[] chars = HttpUtility.HtmlEncode( text ).ToCharArray();
    StringBuilder result = new StringBuilder( text.Length + (int)( text.Length * 0.1 ) );

    foreach ( char c in chars ) {
        int value = Convert.ToInt32( c );
        if ( value > 127 )
            result.Append( c );

    return result.ToString();

Note that we call the built-in System.Web.HttpUtility.HtmlEncode() beforehand which is a prerequisite since it covers the encoding of ASCII-based control characters such as the ampersand (&) and less-than/greater-than symbols (<,>).

Points of Interest

This code is admittedly very hackish, however the only point of concern I have with it is ensuring that the numerical representation of the character is in fact standard Unicode values. In my tests, I found this to be the case, however I would like to guarantee it. If anyone knows the answer to this, please post a message in the comments and I'll update the article.


  • Aug 28, 2007 - First post
  • Aug 29, 2007 - Thanks to J4amieC for the StringBuilder optimization. I decided to start it off with a buffer of 10% on top of the passed string length, which should be more than enough for typical usage.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

President The Little Software Company
Canada Canada
My name is Logan Murray and I'm a Canadian. I'm interested primarily in C# and Windows desktop application development (learning WPF at the moment and then hopefully LINQ), though I hope to branch-out more to the web side of things eventually. I am the president and owner of The Little Software Company and am currently working on the Windows version of OrangeNote, to be released soon. Check out my RSS reader, FeedBeast™.

Comments and Discussions

Questioncharacters < 31? Pinmemberitamar8211-Mar-13 3:49 
GeneralGreat PinmemberMember 774517327-Apr-11 6:09 
Generalamazing ! thanks so much Pinmemberdaniel77021-Feb-11 10:43 
GeneralThe same but as a method extension PinmemberGeni19-Feb-10 0:23 
GeneralPrevent conversion of HTML Tags Pinmembergibberishh13-Jan-10 2:19 
GeneralRe: Prevent conversion of HTML Tags Pinmemberchaiguy133713-Jan-10 7:50 
GeneralRe: Prevent conversion of HTML Tags Pinmembergibberishh14-Jan-10 18:54 
Generalnew lines, spacing [modified] Pinmembergordonmt9-Jun-09 12:47 
GeneralProperly Encoding for HTML PinmemberJHankins4-Sep-07 7:50 
GeneralRe: Properly Encoding for HTML Pinmemberlogan13374-Sep-07 10:13 
GeneralUnicode point values Pinmemberjluber4-Sep-07 2:22 
GeneralRe: Unicode point values Pinmemberlogan13374-Sep-07 5:40 
GeneralNice and neat Pinmembercraigd3-Sep-07 11:21 
GeneralRe: Nice and neat Pinmemberlogan13373-Sep-07 12:19 
GeneralConvert.ToInt32() Pinmemberaprenot29-Aug-07 7:47 
GeneralRe: Convert.ToInt32() Pinmemberlogan133729-Aug-07 10:49 
QuestionNeed to call HttpUtility.HtmlEncode? PinmemberJcmorin29-Aug-07 2:40 
AnswerRe: Need to call HttpUtility.HtmlEncode? Pinmemberlogan133729-Aug-07 7:13 
GeneralString concatenation PinmvpJ4amieC28-Aug-07 23:39 
GeneralRe: String concatenation Pinmemberlogan133729-Aug-07 7:30 
GeneralRe: String concatenation [modified] PinmemberArjan Einbu4-Sep-07 0:15 
GeneralRe: String concatenation Pinmemberlogan13374-Sep-07 5:15 
AnswerRe: String concatenation PinmemberArjan Einbu4-Sep-07 13:19 
Questioncool, thank you, but reverse? Pinmembersekretar28-Aug-07 21:13 
AnswerRe: cool, thank you, but reverse? Pinmemberlogan133729-Aug-07 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 28 Aug 2007
Article Copyright 2007 by chaiguy1337
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid