Hex Escape - CSharp System

CSharp examples for System:Hex

Description

Hex Escape

Demo Code

// Licensed under the same terms of ServiceStack: new BSD license.
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w  w  w  . j  a v a2 s .  co  m*/

public class Main{
        public static string HexEscape(this string text, params char[] anyCharOf)
		{
			if (string.IsNullOrEmpty(text)) return text;
			if (anyCharOf == null || anyCharOf.Length == 0) return text;

			var encodeCharMap = new HashSet<char>(anyCharOf);

			var sb = new StringBuilder();
			var textLength = text.Length;
			for (var i=0; i < textLength; i++)
			{
				var c = text[i];
				if (encodeCharMap.Contains(c))
				{
					sb.Append('%' + ((int)c).ToString("x"));
				}
				else
				{
					sb.Append(c);
				}
			}
			return sb.ToString();
		}
}

Related Tutorials