C# URL/HTML Encode与Decode
Encode
public static string UrlEncode(string str)
{StringBuilder sb = new StringBuilder();byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);for (int i = 0; i < byStr.Length; i++){sb.Append(@"%" + Convert.ToString(byStr[i], 16));//将 8 位无符号整数的值转换为其等效的指定基数的字符串表示形式。(url转成16进制等效值。范围:2、8、10、16)}return sb.ToString();
}
Encode & Decode
使用C#自带方法
HtmlEncode与HtmlDecode:转换html,转化回车和空格的功能,可以将textarea中输入的文本按照原样在html中显示。简单的理解,就是将特殊html的字符实体与字符相互转换。
参考
//原型public static unsafe void HtmlEncode(string value, TextWriter output) {if (value == null) {return;}if (output == null) {throw new ArgumentNullException("output");}int index = IndexOfHtmlEncodingChars(value, 0);if (index == -1) {output.Write(value);return;}Debug.Assert(0 <= index && index <= value.Length, "0 <= index && index <= value.Length");UnicodeEncodingConformance encodeConformance = HtmlEncodeConformance;int cch = value.Length - index;fixed (char* str = value) {char* pch = str;while (index-- > 0) {output.Write(*pch++);}for (; cch > 0; cch--, pch++) {char ch = *pch;if (ch <= '>') {switch (ch) {case '<':output.Write("<");break;case '>':output.Write(">");break;case '"':output.Write(""");break;case '\'':output.Write("'");break;case '&':output.Write("&");break;default:output.Write(ch);break;}}else {int valueToEncode = -1; // set to >= 0 if needs to be encoded#if ENTITY_ENCODE_HIGH_ASCII_CHARSif (ch >= 160 && ch < 256) {// The seemingly arbitrary 160 comes from RFCvalueToEncode = ch;} else
#endif // ENTITY_ENCODE_HIGH_ASCII_CHARSif (encodeConformance == UnicodeEncodingConformance.Strict && Char.IsSurrogate(ch)) {int scalarValue = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref cch);if (scalarValue >= UNICODE_PLANE01_START) {valueToEncode = scalarValue;}else {// Don't encode BMP characters (like U+FFFD) since they wouldn't have// been encoded if explicitly present in the string anyway.ch = (char)scalarValue;}}if (valueToEncode >= 0) {// value needs to be encodedoutput.Write("&#");output.Write(valueToEncode.ToString(NumberFormatInfo.InvariantInfo));output.Write(';');}else {// write out the character directlyoutput.Write(ch);}}}}}
//Html编码
System.Web.HttpUtility.HtmlEncode(str);
//Html解码
System.Web.HttpUtility.HtmlDecode(str);//Url编码
System.Web.HttpUtility.UrlEncode(str);
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.Unicode);
//Url解码
System.Web.HttpUtility.UrlDecode(str);
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.Unicode);
C# URL/HTML Encode与Decode相关推荐
- Url路径处理encode和decode
Url路径处理encode和decode 1.url路径进行encode 2.url路径进行decode 1.url路径进行encode 1.1浏览器自动encode 当我们用浏览器测试自己开发的ap ...
- 2019年2月26日 Unique Email Addresses、To Lower Case、Encode and Decode TinyURL
今天开始加快速度,趁着还有空多刷几题,语言换成python提高速度了. 1. Unique Email Addresses 弱题,注意@符号前后的处理方式不同 class Solution(objec ...
- LeetCode 535. Encode and Decode TinyURL
题目: Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL ...
- 【leetcode】535. Encode and Decode TinyURL
原题 TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/d ...
- go 基于gin编写encode、decode、base64加密接口
GoLang学习更方便,一文在手,面试我有 GoLang面试 | GoLang面试 | GoLang面试 文章目录 前言 代码 演示 前言 基于gin编写encode.decode.base64加密接 ...
- 49 Encode and Decode TinyURL
题目 Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL ...
- python3 的encode 和 decode
str ----> bytes: encode 编码 bytes----> str: decode 解码 str.encode() bytes.decode() >>> ...
- decode函数python在哪里_Python基础知识——encode和decode函数
以前我们介绍过,Python2.x中默认的编码的基础类型是unicode编码的类型,在Python3.x才转化为基于unicode的字符串. 那么我们在Python2.x的学习中就会遇到各种各样的编码 ...
- pythonunicode和str_python的str,unicode对象的encode和decode方法
python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...
最新文章
- BrainNet:用于人与人之间直接协作的多人脑对脑接口
- 自考计算机和行政管理哪个好考,自考行政管理好考吗?自考行政管理都考哪些科目?...
- 知识图谱(六)——关系抽取
- 老婆从怀孕到产子的所有细节
- nginx 禁止通过IP,未绑定域名访问服务器
- hybris commerce storefront的产品搜索功能
- java存入光盘_java 这是 学习笔记(jdk7)书中的光盘里的源码,不知大家需要不,里面都是新手 的好 Develop 238万源代码下载- www.pudn.com...
- 河南农大姚文与中科院北京基因组所章张课题组合作发布真核生物长链反向重复序列数据库...
- 测绘工程野外测量实践实习
- MAC地址前三位是厂家标识符(Organizationally Unique Identifier),可以从IEEE官网查询
- AGM FPGA选型手册
- yii2 restful web服务[格式响应]
- Word删除空白页方法,由分节符导致多出空白页删除方法
- 计算机无法识别苹果6手机,苹果手机连接电脑没反应,小编教你苹果手机连接电脑没反应怎么办...
- docker部署tf-serving模型
- MBR15200FAC-ASEMI插件肖特基二极管MBR15200FAC
- Windows 2003安全设置大全----2003系统错误大全解释
- 微信小程序 - 婚礼邀请函
- 当ChatGPT遇上StableDiffusion ChatGPT指导StableDiffusion绘画
- 360手机官方刷机教程(N5系列通用)