Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by mrdoktor1974 · Apr 26, 2012 at 08:30 AM · htmlcharactersencode

decode html characters in c#

Anyone know how to decode html character codes such as ( & # x 0 0 4 0; ) in C# in Unity?

All examples I can find are using libraries that arent included.

Or maybe an idea on which library to include and how ;)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Apr 26, 2012 at 11:13 AM

Google is your friend:

http://stackoverflow.com/questions/1631819/htmlencode-in-c-sharp

Anyway, if you're just interested in the unicode escape sequences you can convert them yourself.

  1. Search for "&#x" in the srting

  2. extract the numbers until ";"

  3. convert the hexnumber string to an int

  4. cast the int to char and you get your character

I would do something like this:

 string DecodeHtmlChars(string aText)
 {
     string[] parts = aText.Split(new string[]{"&#x"}, StringSplitOptions.None);
     for (int i = 1; i < parts.Length; i++)
     {
         int n = parts[i].IndexOf(';');
         string number = parts[i].Substring(0,n);
         try
         {
             int unicode = Convert.ToInt32(number,16)
             parts[i] = ((char)unicode) + parts[i].Substring(n+1);
         } catch {}
     }
     return String.Join("",parts);
 }

Haven't tested it yet, but should work ;)

edit Just remembered that the number is a hexadecimal string. Changed the code...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 26, 2012 at 11:36 AM 1
Share

Note that there are also other escape sequences like "`&`". There's also the decimal unicode format "`@`" == "`@`"

avatar image mrdoktor1974 · Apr 26, 2012 at 12:34 PM 1
Share

After fixing a few "writing errors" (Length with capitol L and space between new string[] and {"&#x"} it works just fine. Thank you so much!

Here's my string extension class I made from your example:

 using UnityEngine;

using System; using System.Collections;

public static class StringExtensions {

 public static string DecodeHtmlChars(this string source)
 {
     string[] parts = source.Split(new string[] {"&#x"}, StringSplitOptions.None);
     for (int i = 1; i < parts.Length; i++)
     {
         int n = parts[i].IndexOf(';');
         string number = parts[i].Substring(0,n);
         try
         {
             int unicode = Convert.ToInt32(number,16);
             parts[i] = ((char)unicode) + parts[i].Substring(n+1);
         } catch {}
     }
     return String.Join("",parts);
 }

}

avatar image Bunny83 · Apr 26, 2012 at 02:16 PM 0
Share

@Breakmachine: Yes, thanks for the hints. Recently i've done a lot LUA program$$anonymous$$g so i'm a bit rusty with .NET / C# and i've written this from scratch ;)

btw. you don't need a space between the array type and the initializer brackets, but i forgot the StringSplitOptions. I've fixed my code.

If you want to handle all variants you could split at "&", copy the string until ";" and then check for the 3 different methods: hexstring (starts with "x"), decimal number, one of those keywords

avatar image
1

Answer by Miraculade · Aug 22, 2016 at 07:22 PM

Someone grabbed the Mono-HttpUtility to use it in Unity: https://github.com/Cratesmith/RestSharp-for-unity3d/tree/master/RestSharp/Extensions/MonoHttp

Import all three classes into your project, and use the RestSharp.Contrib namespace to call HttpUtility.HtmlDecode (yourHtmlTextToDecode);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by devilkkw · Apr 26, 2012 at 10:36 AM

 string aaa = myText.Text;//text to decode.
 string b = aaa.Replace("&", "WAT YOU WANT");
 myText.Text=b;

just use it on a void called by a button click. We haven't more datails for give best help: have u to replace stirung in a label,in a butto,or in other?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 26, 2012 at 11:30 AM 0
Share

Well, he don't want to just replace a string. It's about the unicode html encoding format. The sequence "@" is the character "@" for example.

http://en.wikipedia.org/wiki/Unicode_block

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help me about this problem 0 Answers

How to load html content 2 Answers

Need to add sub-total type running total to a form with radio buttons 0 Answers

Unable to connect to Update Server 1 Answer

how to use Iframe 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges