How To Convert Currency Symbol To Corresponding Html Entity
Solution 1:
HtmlEncode
only looks for a few special characters and replaces them with hard-coded values, and additionally a few more high ASCII characters (160 - 255) as described here. The only way to encode into entity names is by specifying them manually. I gave it a shot and built a wrapper around the System.Net.WebUtility
class while leveraging the existing Html entities dataset used by .NET to decode as well so that decoding continues to work with this solution. I've hosted it on github: WebUtilityWrapper. You can use it as shown below:
WebUtilityWrapper.HtmlEncode("€"); // Returns €
WebUtilityWrapper.HtmlEncode("Δ"); // Returns Δ
WebUtilityWrapper.HtmlEncode("&"); // Returns &
WebUtilityWrapper.HtmlEncode("$"); // Returns $
WebUtilityWrapper.HtmlEncode("€¢£¥"); // Returns €¢£¥
I've tested it out by encoding and decoding back and then verifying if we get the original string for a large set of unicode characters. Sharing some more tests:
HtmlEncode() Response using framework's HtmlEncode: (link)
// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~
// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°±´µ¶¸ºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
// Unicodes for Greek Alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝ
// Unicodes for 9824 - 9830
♠♣♥♦
HtmlEncode() Response using WebUtilityWrapper.HtmlEncode:
// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~
// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°±´µ¶¸ºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
// Unicodes for Greek alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ
// Unicodes for 9824 - 9830
♠♣♥♦
Hope this helps!
Solution 2:
HtmlEncode
and HtmlDecode
are not symmetric.
HtmlEncode
will only encode a handful of special characters:
<
=><
>
=>>
&
=>&
'
=>'
"
=>"
and for some unicode characters it will convert them to the &#<num>;
format.
So no conversions into HtmlEntities takes place.
Post a Comment for "How To Convert Currency Symbol To Corresponding Html Entity"