An open API service indexing awesome lists of open source software.

https://github.com/hon-key/gbkinvalidcharfilter

a example to filter Invalid character in gb18030 String using Objective-C
https://github.com/hon-key/gbkinvalidcharfilter

Last synced: about 1 month ago
JSON representation

a example to filter Invalid character in gb18030 String using Objective-C

Awesome Lists containing this project

README

        

# GBKInvalidCharFilter
a example to filter Invalid character in gb18030 String using Objective-C

# Intro
有不少 html 使用了 GB18030 编码页面,在解析过程中,我们经常得到 nil 值,这是因为里面可能含有非法字符造成的,网上解决这中非法字符的例子不是太直观,通过该例子可以较为直观地解释过滤非法字符的过程

我们在解析 GB18030 的时候,需要注意到 GB18030 使用了单字节、双字节以及四字节的编解码规则。

* 单字节就是 ASCII 码编解码规则
* 双字节,高位为 [0x81 ~ 0xfe];低位为 [0x40 ~ 0x7e] 或 [0x80 ~ 0xfe]
* 四字节,高位往下分别是 [0x81 ~ 0xfe]、[0x30 ~ 0x39]、[0x81 ~ 0xfe] 、 [0x30 ~ 0x39]

沿着这个思路,我们可以过滤掉所有不包含在这里里面的非法字节,统一展示位问号,这也是众多浏览器所使用的方法。