一. Base64 编码

Base64 是许多 web 协议的标准,在日常开发中很多地方会需要利用 Base64 进行编解码的操作;

在相互转换操作的时候,可以借用 NSData 来执行,例如接口中提供的如下方法:

/* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized as valid Base-64.
*/
- (nullable instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));/* Create a Base-64 encoded NSString from the receiver's contents using the given options.
*/
- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

其中,第一个方法用来编码时操作,官方大致意思为将一个字符串(NSString)类型的参数通过编码后转换为 NSData 类型,若所输入的数据未能被识别为有效 Base-64 时,则返回 nil.

第二个方法是将 NSData 类型数据通过 Base-64 的方式解码为字符串类型参数.

具体使用方式 code 如下:

1.Data 转 String

NSData *data = [[NSData alloc] initWithBase64EncodedString:@"Encoded String"options:0];//编码
NSString *decodeStr = [data base64EncodedStringWithOptions:0];//解码
NSLog(@"data --- %@/n decodeStr --- %@", data, decodeStr);

2.字符串与 Data 间相互转换

NSString *encodeStr = @"Encode String Test";
NSData *encodeData = [[encodeStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedDataWithOptions:0];// 编码
NSData *decodeData = [[NSData alloc] initWithBase64EncodedData:encodeDataoptions:0];// 解码
NSString *decodeStr = [[NSString alloc] initWithData:decodeDataencoding:NSUTF8StringEncoding];// Data 转 String
NSLog(@"编码:\n%@解码:\n%@Data to change String:\n%@", encodeData, decodeData, decodeStr);

二.百分号编码

百分号编码对 web 协议也很重要,尤其是对 URL 的处理上;

其中官方 API 提供的接口方法如下:

// Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));// Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

其中,第一项为将输入的字符串内容通过百分号编码序列后生成一个编码后的新字符串;

第二项与第一项类似,同样是将输入的字符串内容通过百分号编码序列的方式编码后生成一个新的字符串,但该方法允许开发者控制需要百分号编码的字符,目的是成一个 URL 字符串编码组件或子组件,而不是整个 URL 字符串.在7位 ASCII 范围外的任何字符被忽略.
具体使用方式 code 如下:

NSString *testStr = @"Percent Test";
NSString *encodStr = [testStr stringByRemovingPercentEncoding];// 编码
NSString *encodCharacters = [testStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];// 编码并控制所需百分号编码的字符NSLog(@"\nPercentEncoding:\n%@\nPercentEncodingAndCharacters:\n%@", encodStr, encodCharacters);

打印结果如下:

Base64与百分号编码相关 - iOS相关推荐

  1. base64编码相关-btoa和atob及中文乱码报错问题

    base64编码相关-btoa和atob及中文乱码报错问题 最近在做二进制编码相关的东西,关于Base64的编码解码问题.遇到了一些问题. btoa(); btoa函数全称就是Binary-to-AS ...

  2. c语言 字符串 url,如何对URL字符串进行百分号编码

    在和web服务进行交互时,我们经常需要对URL中的特定字符和传输的表单数据进行百分号编码.例如,'&'在百分号编码时会变成'%26'.搞清楚 URL中哪部分的哪些字符应该进行百分号编码了并不是 ...

  3. Base64和URL编码

    URL为什么使用百分号编码(URL编码) 1.一些数据(二进制串)无法在URL中使用字符表示(以US-ASCII字符集举例,其中的一些 字符 并不能显示的表示出来,如响铃) 2.传输的字符是URL中保 ...

  4. 便利贴--41{js - base64 - _utf8_ 的编码和解码}

    便利贴--41{便利贴--41{js - base64 - _utf8_ 的编码和解码}} 编码 解码 utf8 合并 编码 this.encode = function (input) {_keyS ...

  5. 百分号编码和汉字的转换

    from urllib.parse import quote,unquotequote('汉字')unquote('百分号编码')

  6. java服务器端开发-servlet:202、Servlet执行过程介绍:get请求与post请求、编码相关等

    声明:本教程不收取任何费用,欢迎转载,尊重作者劳动成果,不得用于商业用途,侵权必究!!! 文章目录 4.get请求与post请求 (1)哪一些情况下,浏览器会发送get请求? (2)哪一些情况下,浏览 ...

  7. nodejs处理url中的百分号编码

    nodejs处理url中的百分号编码 写网站的时候,前端表单发送数据到nodejs服务器,发送的数据会出现一些乱码,像下面这样: nickname=%E5%B0%8F%E5%88%98&gen ...

  8. Base64编码相关知识总结

    Base64编码是什么? Base64,顾名思义,就是包括小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"一共64个字符的字符集,(另加一个&qu ...

  9. linux shell base64 加解密 编码字符串

    实例 加密: $ echo Hello World | base64 SGVsbG8gV29ybGQK 解密: $ echo SGVsbG8gV29ybGQK | base64 -d Hello Wo ...

最新文章

  1. 利用追赶法来求解方程Ax=b的C++程序
  2. 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?
  3. 2021年春季学期-信号与系统-第四次作业参考答案-第二小题
  4. Lambda 表达式(=):网络摘抄,自学用,侵删。
  5. 实验一 OpenGL初识
  6. B10_NumPy数组操作、修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除
  7. python学习笔记--迭代器
  8. wireshark-win64-3.4.0安装_轴承安装规范
  9. java源程序编译型_Java语言的源程序不是编译型的,而是编译解释型的。
  10. 标注所有线段的lisp程序源码_仪表进近图剖面图的详解(五)进近程序高度限制...
  11. 【iOS开发】如何用 Swift 语言进行LBS应用的开发?
  12. Pandas入门教程(五)
  13. 【面试感悟】一名3年工作经验的程序员应该具备的技能
  14. 深度深度网络设计中各种归一化方法总结
  15. 算法竞赛入门经典(刘汝佳)——基础篇心得
  16. 音频技术的下一个“热点”,会出现在哪个领域?丨一期一会 • 音频工程师专场
  17. 长尾效应解析以及长尾效应在电商中的应用
  18. 改进YOLOv7系列:首发结合最新Centralized Feature Pyramid集中特征金字塔,通过COCO数据集验证强势涨点
  19. 对视频的分辨率大小进行裁剪
  20. GitLab Projects 2020 插件配置

热门文章

  1. 网页设计图片向上浮动_CSS 关于浮动
  2. [有明信息]横向集成、纵向贯通 ——地产财务管理新思路
  3. [竹马推荐]2005年IT人才需求趋势 (1)(2)
  4. NANK南卡护眼台灯Pro全面评测:旗舰级护眼天花板!
  5. 3、MySQL 高级-用户与权限管理
  6. OceanMind海睿思入选《2023中国企业数智化转型全景图中国数据智能产业图谱》
  7. java计算机毕业设计康养旅游信息系统源码+数据库+系统+lw文档+mybatis+运行部署
  8. Freemarker 运算符
  9. 一起学爬虫(Python) — 16 梅开二度,猫眼票房!
  10. 修改CS、IP的指令与jmp指令