private Dictionary <string, object> encode(string text) { var d = new Dictionary <string, object>(); text = text ?? string.Empty; var first = true; var charOrig = new List <string>(); Func <string, string> toPropName = (en) => { en = en.Replace("-", string.Empty).Replace("unicode", "ucs2"); return(en[0].ToString().ToUpper() + en.Substring(1)); }; foreach (var encName in encNames) { var enc = Encoding.GetEncoding(encName); var fixEncName = encName.Replace("unicode", "ucs2").Replace("-", string.Empty).FirstCharToUpper(); d.Add( "code" + fixEncName, getHexString(text, encName)); d.Add("pvw" + fixEncName, enc.GetString(enc.GetBytes(text))); //add grouped as char results var charGroups = new List <string[]>(); var i = 0; while (i < text.Length) { //check if surrogate pairs var c = text[i]; var t = c.ToString(); if (c >= 0xd800 && c <= 0xdbff && i < text.Length - 1 && text[i + 1] >= 0xdc00 && text[i + 1] <= 0xdfff) { t = text.Substring(i, 2); i++; } if (first) { charOrig.Add(t); } charGroups.Add(getByteHexStrings(t, encName)); i++; } first = false; d.Add("chars" + toPropName(encName), charGroups.ToArray()); } d.Add("charsOrig", charOrig); d.Add("ueBig5", EncodeHelper.UrlEncode(text, Encoding.GetEncoding(950))); d.Add("ueUnicode", HttpEncoder.UrlEncodeUnicode(text, true)); d.Add("ueUtf8", EncodeHelper.UrlEncode(text, Encoding.UTF8)); d.Add("codeNcr", EncodeHelper.ConvertToNCR(text)); d.Add("text", text); return(d); }
private Dictionary <string, string> encode(string text) { var d = new Dictionary <string, string>(); text = text ?? string.Empty; foreach (var encName in encNames) { var enc = Encoding.GetEncoding(encName); var fixEncName = encName.Replace("unicode", "ucs2").Replace("-", string.Empty).FirstCharToUpper(); d.Add( "code" + fixEncName, getHexString(text, encName)); d.Add("pvw" + fixEncName, enc.GetString(enc.GetBytes(text))); } d.Add("ueBig5", EncodeHelper.UrlEncode(text, Encoding.GetEncoding(950))); d.Add("ueUnicode", HttpEncoder.UrlEncodeUnicode(text, true)); d.Add("ueUtf8", EncodeHelper.UrlEncode(text, Encoding.UTF8)); d.Add("codeNcr", EncodeHelper.ConvertToNCR(text)); d.Add("text", text); return(d); }