Mounarajan
Mounarajan

Reputation: 1427

Converting unicode not working in c#

I am trying to convert unicode character in c# but it is not working

code:

using System;
using System;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace test_request
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            string unicode = "Not Your Boyfriend's Tunic";
            string str = System.Uri.UnescapeDataString(unicode);


            Console.WriteLine (str);
        }
    }
}

Ouput:

Not Your Boyfriend's Tunic

But expected output is this

Expected output:

Not Your Boyfriend's Tunic 

Upvotes: 1

Views: 793

Answers (3)

Preet
Preet

Reputation: 994

Both solutions will work for you:

  1. Using Server.HtmlDecode:

     string unicode = "Not Your Boyfriend's Tunic";
     string str = Server.HtmlDecode(unicode);
     Console.WriteLine (str);
    

    reference link: This MSDN document.

  2. Using HttpUtility.HtmlDecode: hello

    string unicode = "Not Your Boyfriend's Tunic";    
    string str = HttpUtility.HtmlDecode(unicode);
    Console.WriteLine (str);
    

    reference link: This MSDN document.

Difference between both:

Both does same work but the only difference is: Server.HtmlDecode() is readily availalble at runtime from a web page whereas HttpUtility.HtmlDecode() is a static method that can be used from anywhere.

Upvotes: 2

ali zarei
ali zarei

Reputation: 1241

try this

using System;
using System;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace test_request
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            string unicode = "Not Your Boyfriend's Tunic";
            string str = System.Web.HttpUtility.HtmlDecode(unicode);
            Console.WriteLine (str);
        }
    }
}

Upvotes: 2

slon
slon

Reputation: 1030

Use below function:

HttpUtility.HtmlDecode("Not Your Boyfriend's Tunic");
HttpUtility.HtmlEncode("Not Your Boyfriend's Tunic");

Upvotes: 3

Related Questions