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("&lt;");break;case '>':output.Write("&gt;");break;case '"':output.Write("&quot;");break;case '\'':output.Write("'");break;case '&':output.Write("&amp;");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相关推荐

  1. Url路径处理encode和decode

    Url路径处理encode和decode 1.url路径进行encode 2.url路径进行decode 1.url路径进行encode 1.1浏览器自动encode 当我们用浏览器测试自己开发的ap ...

  2. 2019年2月26日 Unique Email Addresses、To Lower Case、Encode and Decode TinyURL

    今天开始加快速度,趁着还有空多刷几题,语言换成python提高速度了. 1. Unique Email Addresses 弱题,注意@符号前后的处理方式不同 class Solution(objec ...

  3. LeetCode 535. Encode and Decode TinyURL

    题目: Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL ...

  4. 【leetcode】535. Encode and Decode TinyURL

    原题 TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/d ...

  5. go 基于gin编写encode、decode、base64加密接口

    GoLang学习更方便,一文在手,面试我有 GoLang面试 | GoLang面试 | GoLang面试 文章目录 前言 代码 演示 前言 基于gin编写encode.decode.base64加密接 ...

  6. 49 Encode and Decode TinyURL

    题目 Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL ...

  7. python3 的encode 和 decode

    str ----> bytes: encode 编码 bytes----> str: decode 解码 str.encode() bytes.decode() >>> ...

  8. decode函数python在哪里_Python基础知识——encode和decode函数

    以前我们介绍过,Python2.x中默认的编码的基础类型是unicode编码的类型,在Python3.x才转化为基于unicode的字符串. 那么我们在Python2.x的学习中就会遇到各种各样的编码 ...

  9. pythonunicode和str_python的str,unicode对象的encode和decode方法

    python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

最新文章

  1. BrainNet:用于人与人之间直接协作的多人脑对脑接口
  2. 自考计算机和行政管理哪个好考,自考行政管理好考吗?自考行政管理都考哪些科目?...
  3. 知识图谱(六)——关系抽取
  4. 老婆从怀孕到产子的所有细节
  5. nginx 禁止通过IP,未绑定域名访问服务器
  6. hybris commerce storefront的产品搜索功能
  7. java存入光盘_java 这是 学习笔记(jdk7)书中的光盘里的源码,不知大家需要不,里面都是新手 的好 Develop 238万源代码下载- www.pudn.com...
  8. 河南农大姚文与中科院北京基因组所章张课题组合作发布真核生物长链反向重复序列数据库...
  9. 测绘工程野外测量实践实习
  10. MAC地址前三位是厂家标识符(Organizationally Unique Identifier),可以从IEEE官网查询
  11. AGM FPGA选型手册
  12. yii2 restful web服务[格式响应]
  13. Word删除空白页方法,由分节符导致多出空白页删除方法
  14. 计算机无法识别苹果6手机,苹果手机连接电脑没反应,小编教你苹果手机连接电脑没反应怎么办...
  15. docker部署tf-serving模型
  16. MBR15200FAC-ASEMI插件肖特基二极管MBR15200FAC
  17. Windows 2003安全设置大全----2003系统错误大全解释
  18. 微信小程序 - 婚礼邀请函
  19. 当ChatGPT遇上StableDiffusion ChatGPT指导StableDiffusion绘画
  20. 360手机官方刷机教程(N5系列通用)

热门文章

  1. 使用Socks5代理加强爬虫的网络安全
  2. 删除数据库表中重复数据的总结(oracle)
  3. Win11系统电脑开机密码忘记了怎么重置密码?
  4. 诺基亚5230_5235_RM-356刷机包20.6.006版
  5. 中国的情人节(7夕)
  6. 设置Excel单元格背景色(setFillForegroundColor)
  7. cppcheck 自定义规则_cppcheck扫描规则
  8. PX4项目学习::(八)无人机仿真综述(介绍)
  9. uni-app 使用高德地图部分内容
  10. 52-2018 蓝桥杯省赛 B 组模拟赛(一)java