0

Observe the following:

WebUtility.HtmlDecode("\u003d")

Outputs "="

WebUtility.HtmlEncode("=")

Outputs "="

I want to encode a string which contains equal signs, but the sign is not being coded. I don't want to write this functionality myself as I don't yet know all the character cases I have to write for.

Can anyone answer why the HtmlEncode and Decode function seems to not be two-way in this case, and if there is a function I should be using for encoding = to /u003d

dahui
  • 2,128
  • 2
  • 21
  • 40

1 Answers1

1

"\u003d" in C# is the same as "=" (try "\u003d" == "="). Your issue has nothing to do with HTML encode / decode. \u003d is a unicode character - that is the encode/decode you should be searching for.

I have found the following questions which provide code examples for encoding and decoding like you are looking for.

From this answer:

static string EncodeNonAsciiCharacters( string value ) {
    StringBuilder sb = new StringBuilder();
    foreach( char c in value ) {
        if( c > 127 ) {
            // This character is too big for ASCII
            string encodedValue = "\\u" + ((int) c).ToString( "x4" );
            sb.Append( encodedValue );
        }
        else {
            sb.Append( c );
        }
    }
    return sb.ToString();
}

static string DecodeEncodedNonAsciiCharacters( string value ) {
    return Regex.Replace(
        value,
        @"\\u(?<Value>[a-zA-Z0-9]{4})",
        m => {
            return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
        } );
}
Neil
  • 1,613
  • 1
  • 16
  • 18
  • Hmm makes sense. I didn't realise it was a unicode character as only the = sign seems to be being encoded in my example. Thanks for the answer – dahui Oct 24 '18 at 13:47