zoukankan      html  css  js  c++  java
  • JavaScript中的常用编码

    参考:

        文章并非原创,这里只是做了个简单整理。

    一些名词

    bit
    二进制数字,称作比特
    byte
    字节,1byte = 8bits。这和ASCII有关,最初ASCII包含128个常用字符,128=2的7次方+1个补码反码表示负数什么的,因此每个字符需要8个二进制来表示。
    字符集
    charset,也就是某个符号和某个数字映射关系的一个表,也就是它决定了107 是koubei 的 ‘a’,21475 是口碑的“口”,不同的表有不同的映射关系,如 ascii,gb2312,Unicode. 通过这个数字和字符的映射表,我们可以把一个二进制表示的数字转换成某个字符。
    编码方式
    同是对于应“口”的 21475 这个数,我们是用 \u5k3e3 表示呢,还是用 %E5%8F%A3 来表示呢?这就是由 character encoding 来决定
    Unicode
    UCS, Universal Character Set。一个囊括所有字符在内的字符集和对应编码方式的标准
    gbk, gb2312, utf-8
    gbk,gb2312是字符集 (charset)
    utf-8 是一种编码方式 (character encoding) ,是 Unicode 标准中 UCS 字符集的一种编码方式
    Percent-encoding
    Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI) under certain circumstances. Although it is known as URL encoding it is, in fact, used more generally within the main Uniform Resource Identifier (URI) set, which includes both Uniform Resource Locator (URL) and Uniform Resource Name (URN). As such it is also used in the preparation of data of the "application/x-www-form-urlencoded" media type, as is often used in email messages and the submission of HTML form data in HTTP requests.
    http://en.wikipedia.org/wiki/Percent-encoding

    它有两部分字符组成:保留字符和非保留字符。

    保留字符:! * ‘ ( ) ; : @ & = + $ , / ? % # [ ]
    保留字符,是有特殊意义的,如果在不代表那些特殊意义而代表原意的时候出现,必须经过编码。例如1 ==> %21

    非保留字符:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    0 1 2 3 4 5 6 7 8 9 – _ . ~

    javascript的内置函数 encodeURI、decodeURI、encodeURIComponent、decodeURIComponent 就是进行的 Percent Encode,只是在对待 : / ; ?等特殊字符的时候有区别。

    JavaScript:escape, encodeURIComponent和encodeURI

    对于ASCII字符
    这三个函数的作用都是将字符转换成百分比编码(Percent-encoding),区别是各自排除编码的字符不同:
    escape()  will not encode:  @ * / +
    encodeURI()  will not encode:  ~ ! @ # $ & * ( ) = : / , ; ? + '
    encodeURIComponent()  will not encode:  ~ ! * ( ) '
    对于非ASCII字符
    escape和encodeURIComponent差异比较大:
    escape('一') == '%u04BB'
    encodeURI('一') == '%u04BB'
    encodeURIComponent('一') == '%u04BB'
    
    escape('康') == '%uFFFD%uFFFD'
    encodeURI('康') == '%EF%BF%BD%EF%BF%BD'
    encodeURIComponent('康') == '%EF%BF%BD%EF%BF%BD'
    

    escape的返回值是非标准Pecent-encoding, 现在已经没有标准支持,建议少用

    encodeURI和encodeURIComponent

    主要区别:encodeURIComponent比encodeURI转码的字符要多一部分,
    包括:@ # $ & = : / , ; ? +

    之所以有上面两个不同的函数,是因为我们在写JS代码的时候对URI进行两种不同的编码处理需求。
    encodeURI可以用来对完整的URI字符串进行编码处理。
    而encodeURIComponent可以对URI中一个部分进行编码,从而让这一部分可以包含一些URI保留字符。
    参考:javascript encodeURI和encodeURIComponent的比较

  • 相关阅读:
    详细讲解Linux下安装python3(Python3.5.4)
    JavaScript抽象语法树英文对照
    关于MacBook Pro外接4K/60HZ显示器的问题
    Vue组件v-if新渲染的组件不更新
    Vue 子组件与子组件之间传值
    Spring整合CXF步骤,Spring实现webService,spring整合WebService
    CXF错误:Unsupported major.minor version 51.0,java.lang.UnsupportedClassVersionErro
    springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
    jquery.validate中使用remote,remote相同值不校验问题解决
    MyBatis返回主键,MyBatis Insert操作返回主键
  • 原文地址:https://www.cnblogs.com/rainman/p/1877469.html
Copyright © 2011-2022 走看看