1

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 
Mounarajan
  • 1,357
  • 5
  • 22
  • 43

3 Answers3

3

Use below function:

HttpUtility.HtmlDecode("Not Your Boyfriend's Tunic");
HttpUtility.HtmlEncode("Not Your Boyfriend's Tunic");
slon
  • 1,002
  • 1
  • 8
  • 12
2

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.

Preet
  • 984
  • 2
  • 14
  • 34
2

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);
        }
    }
}
ali zarei
  • 1,212
  • 11
  • 17