I have a string like Feel
and I want to decode it to its ascii representation - feel
.
Is there any library in C# that does it, or I have to do it manually?
Asked
Active
Viewed 1,702 times
1
-
Do you really want ASCII? Or do you want a .NET/C# string (which is UTF-8)? – Dec 27 '13 at 18:21
-
@elgonzo UTF-8 will be fine. – Lior Dec 27 '13 at 18:21
-
I'm not sure exactly what the limits of this are, but you can (possibly) try HtmlDecode: http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx – drew_w Dec 27 '13 at 18:22
-
It is probably better to use `WebUtility.HtmlDecode` than `HttpUtility.HtmlDecode`. See [here for explanation](http://stackoverflow.com/a/122658/2819245). – Dec 27 '13 at 18:24
-
1@elgonzo .NET strings aren’t UTF-8, they’re UTF-16. – Konrad Rudolph Dec 27 '13 at 18:24
-
@Konrad: Ooops, ofcourse UTF-16... – Dec 27 '13 at 18:26
3 Answers
3
To decode the string, use WebUtility.HtmlDecode.
Here's a sample LINQPad program that demonstrates:
void Main()
{
string s = "Feel";
string decoded = WebUtility.HtmlDecode(s);
decoded.Dump();
}
Output:
Feel
Note: You're missing a semicolon from the string you've presented in the question. Without the final semicolon, the output will be:
Feel

Lasse V. Karlsen
- 380,855
- 102
- 628
- 825
1
You can use the following code, here's a console sample:
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication
{
class Program
{
public static String ReplaceASCIICodesWithUTF(String target)
{
Regex codeSequence = new Regex(@"&#[0-9]{1,3};");
MatchCollection matches = codeSequence.Matches(target);
StringBuilder resultStringBuilder = new StringBuilder(target);
foreach (Match match in matches)
{
String matchedCodeExpression = match.Value;
String matchedCode = matchedCodeExpression.Substring(2, matchedCodeExpression.Length - 3);
Byte resultCode = Byte.Parse(matchedCode);
resultStringBuilder.Replace(matchedCodeExpression, ((Char)resultCode).ToString());
}
return resultStringBuilder.ToString();
}
static void Main(string[] args)
{
String rawData = "Feel";
Console.WriteLine(ReplaceASCIICodesWithUTF(rawData));
}
}
}

Ilya Tereschuk
- 1,204
- 1
- 9
- 21
0
To decode:
then, for example,
GetBytes/GetString
(getbytes on decoded string, then getstring from those bytes)

Hamlet Hakobyan
- 32,965
- 6
- 52
- 68

illegal-immigrant
- 8,089
- 9
- 51
- 84