关于 UTF7 和 UTF8编码的问题

在用到C#中的Encoding类库的时候遇到这两个编码,不懂它们到底是怎么进行编码的,有知道的可以介绍下吗?

UTF8其实和Unicode是同类,就是在编码方式上不同!
首先UTF8编码后的大小是不一定,不像Unicode编码后的大小是一样的!

我们先来看Unicode的编码:一个英文字母 “a” 和 一个汉字 “好”,编码后都是占用的空间大小是一样的,都是两个字节!

而UTF8编码:一个英文字母“a” 和 一个汉字 “好”,编码后占用的空间大小就不样了,前者是一个字节,后者是三个字节!

现在就让我们来看看UTF8编码的原理吧:
因为一个字母还有一些键盘上的符号加起来只用二进制七位就可以表示出来,而一个字节就是八位,所以UTF8就用一个字节来表式字母和一些键盘上的符号。然而当我们拿到被编码后的一个字节后怎么知道它的组成?它有可能是英文字母的一个字节,也有可能是汉字的三个字节中的一个字节!所以,UTF8是有标志位的!

当要表示的内容是 7位 的时候就用一个字节:0******* 第一个0为标志位,剩下的空间正好可以表示ASCII 0-127 的内容。

当要表示的内容在 8 到 11 位的时候就用两个字节:110***** 10****** 第一个字节的110和第二个字节的10为标志位。

当要表示的内容在 12 到 16 位的时候就用三个字节:1110***** 10****** 10****** 和上面一样,第一个字节的1110和第二、三个字节的10都是标志位,剩下的占湔�每梢员硎竞鹤帧?BR>
以此类推:
四个字节:11110**** 10****** 10****** 10******
五个字节:111110*** 10****** 10****** 10****** 10******
六个字节:1111110** 10****** 10****** 10****** 10****** 10******

UTF-7:A Mail-Safe Transformation Format of Unicode(RFC1642)。这是一种使用 7 位 ASCII 码对 Unicode 码进行转换的编码。它的设计目的仍然是为了在只能传递 7 为编码的邮件网关中传递信息。 UTF-7 对英语字母、数字和常见符号直接显示,而对其他符号用修正的 Base64 编码。符号 + 和 - 号控制编码过程的开始和暂停。所以乱码中如果夹有英文单词,并且相伴有 + 号和 - 号,这就有可能是 UTF-7 编码。

关于UTF7的更多资料如下(都是英语的,如果想具体了解再看):
UTF-7

A Mail-Safe Transformation Format of Unicode

Status of this Memo

This memo provides information for the Internet community. This memo
does not specify an Internet standard of any kind. Distribution of
this memo is unlimited.

Abstract

The Unicode Standard, version 2.0, and ISO/IEC 10646-1:1993(E) (as
amended) jointly define a character set (hereafter referred to as
Unicode) which encompasses most of the world's writing systems.
However, Internet mail (STD 11, RFC 822) currently supports only 7-
bit US ASCII as a character set. MIME (RFC 2045 through 2049) extends
Internet mail to support different media types and character sets,
and thus could support Unicode in mail messages. MIME neither defines
Unicode as a permitted character set nor specifies how it would be
encoded, although it does provide for the registration of additional
character sets over time.

This document describes a transformation format of Unicode that
contains only 7-bit ASCII octets and is intended to be readable by
humans in the limiting case that the document consists of characters
from the US-ASCII repertoire. It also specifies how this
transformation format is used in the context of MIME and RFC 1641,
"Using Unicode with MIME".

Motivation

Although other transformation formats of Unicode exist and could
conceivably be used in this context (most notably UTF-8, also known
as UTF-2 or UTF-FSS), they suffer the disadvantage that they use
octets in the range decimal 128 through 255 to encode Unicode
characters outside the US-ASCII range. Thus, in the context of mail,
those octets must themselves be encoded. This requires putting text
through two successive encoding processes, and leads to a significant
expansion of characters outside the US-ASCII range, putting non-
English speakers at a disadvantage. For example, using UTF-8 together

with the Quoted-Printable content transfer encoding of MIME
represents US-ASCII characters in one octet, but other characters may
require up to nine octets.

Overview

UTF-7 encodes Unicode characters as US-ASCII octets, together with
shift sequences to encode characters outside that range. For this
purpose, one of the characters in the US-ASCII repertoire is reserved
for use as a shift character.

Many mail gateways and systems cannot handle the entire US-ASCII
character set (those based on EBCDIC, for example), and so UTF-7
contains provisions for encoding characters within US-ASCII in a way
that all mail systems can accomodate.

UTF-7 should normally be used only in the context of 7 bit
transports, such as mail. In other contexts, straight Unicode or
UTF-8 is preferred.

See RFC 1641, "Using Unicode with MIME" for the overall specification
on usage of Unicode transformation formats with MIME.

Definitions

First, the definition of Unicode:

The 16 bit character set Unicode is defined by "The Unicode
Standard, Version 2.0". This character set is identical with the
character repertoire and coding of the international standard
ISO/IEC 10646-1:1993(E); Coded Representation Form=UCS-2;
Subset=300; Implementation Level=3, including the first 7
amendments to 10646 plus editorial corrections.

Note. Unicode 2.0 further specifies the use and interaction of
these character codes beyond the ISO standard. However, any valid
10646 sequence is a valid Unicode sequence, and vice versa;
Unicode supplies interpretations of sequences on which the ISO
standard is silent as to interpretation.

Next, some handy definitions of US-ASCII character subsets:

Set D (directly encoded characters) consists of the following
characters (derived from RFC 1521, Appendix B, which no longer
appears in RFC 2045): the upper and lower case letters A through Z
and a through z, the 10 digits 0-9, and the following nine special
characters (note that "+" and "=" are omitted):

Character ASCII & Unicode Value (decimal)
' 39
( 40
) 41
, 44
- 45
. 46
/ 47
: 58
? 63

Set O (optional direct characters) consists of the following
characters (note that "\" and "~" are omitted):

Character ASCII & Unicode Value (decimal)
! 33
" 34
# 35
$ 36
% 37
& 38
* 42
; 59
< 60
= 61
> 62
@ 64
[ 91
] 93
^ 94
_ 95
' 96
{ 123
| 124
} 125

Rationale. The characters "\" and "~" are omitted because they are
often redefined in variants of ASCII.

Set B (Modified Base 64) is the set of characters in the Base64
alphabet defined in RFC 2045, excluding the pad character "="
(decimal value 61).

Rationale. The pad character = is excluded because UTF-7 is designed
for use within header fields as set forth in RFC 2047. Since the only
readable encoding in RFC 2047 is "Q" (based on RFC 2045's Quoted-
Printable), the "=" character is not available for use (without a lot
of escape sequences). This was very unfortunate but unavoidable. The
"=" character could otherwise have been used as the UTF-7 escape
character as well (rather than using "+").

Note that all characters in US-ASCII have the same value in Unicode
when zero-extended to 16 bits.

UTF-7 Definition

A UTF-7 stream represents 16-bit Unicode characters using 7-bit US-
ASCII octets as follows:

Rule 1: (direct encoding) Unicode characters in set D above may be
encoded directly as their ASCII equivalents. Unicode characters in
Set O may optionally be encoded directly as their ASCII
equivalents, bearing in mind that many of these characters are
illegal in header fields, or may not pass correctly through some
mail gateways.

Rule 2: (Unicode shifted encoding) Any Unicode character sequence
may be encoded using a sequence of characters in set B, when
preceded by the shift character "+" (US-ASCII character value
decimal 43). The "+" signals that subsequent octets are to be
interpreted as elements of the Modified Base64 alphabet until a
character not in that alphabet is encountered. Such characters
include control characters such as carriage returns and line
feeds; thus, a Unicode shifted sequence always terminates at the
of a line. As a special case, if the sequence terminates with the
character "-" (US-ASCII decimal 45) then that character is
absorbed; other terminating characters are not absorbed and are
processed normally.

Note that if the first character after the shifted sequence is "-"
then an extra "-" must be present to terminate the shifted
sequence so that the actual "-" is not itself absorbed.

Rationale. A terminating character is necessary for cases where
the next character after the Modified Base64 sequence is part of
character set B or is itself the terminating character. It can
also enhance readability by delimiting encoded sequences.

Also as a special case, the sequence "+-" may be used to encode
the character "+". A "+" character followed immediately by any
character other than members of set B or "-" is an ill-formed
sequence.

Unicode is encoded using Modified Base64 by first converting
Unicode 16-bit quantities to an octet stream (with the most
significant octet first). Surrogate pairs (UTF-16) are converted
by treating each half of the pair as a separate 16 bit quantity
(i.e., no special treatment). Text with an odd number of octets is
ill-formed. ISO 10646 characters outside the range addressable via
surrogate pairs cannot be encoded.

Rationale. ISO/IEC 10646-1:1993(E) specifies that when characters
the UCS-2 form are serialized as octets, that the most significant
octet appear first. This is also in keeping with common network
practice of choosing a canonical format for transmission.

Rationale. The policy for code point allocation within ISO 10646
and Unicode is that the repertoires be kept synchronized. No code
points will be allocated in ISO 10646 outside the range
addressable by surrogate pairs.

Next, the octet stream is encoded by applying the Base64 content
transfer encoding algorithm as defined in RFC 2045, modified to
omit the "=" pad character. Instead, when encoding, zero bits are
added to pad to a Base64 character boundary. When decoding, any
bits at the end of the Modified Base64 sequence that do not
constitute a complete 16-bit Unicode character are discarded. If
such discarded bits are non-zero the sequence is ill-formed.

Rationale. The pad character "=" is not used when encoding
Modified Base64 because of the conflict with its use as an escape
character for the Q content transfer encoding in RFC 2047 header
fields, as mentioned above.

Rule 3: The space (decimal 32), tab (decimal 9), carriage return
(decimal 13), and line feed (decimal 10) characters may be
directly represented by their ASCII equivalents. However, note
that MIME content transfer encodings have rules concerning the use
of such characters. Usage that does not conform to the
restrictions of RFC 822, for example, would have to be encoded
using MIME content transfer encodings other than 7bit or 8bit,
such as quoted-printable, binary, or base64.

Given this set of rules, Unicode characters which may be encoded via
rules 1 or 3 take one octet per character, and other Unicode
characters are encoded on average with 2 2/3 octets per character

plus one octet to switch into Modified Base64 and an optional octet
to switch out.

Example. The Unicode sequence "A<NOT IDENTICAL TO><ALPHA>."
(hexadecimal 0041,2262,0391,002E) may be encoded as follows:

A+ImIDkQ.

Example. The Unicode sequence "Hi Mom -<WHITE SMILING FACE>-!"
(hexadecimal 0048, 0069, 0020, 004D, 006F, 006D, 0020, 002D, 263A,
002D, 0021) may be encoded as follows:

Hi Mom -+Jjo--!

Example. The Unicode sequence representing the Han characters for
the Japanese word "nihongo" (hexadecimal 65E5,672C,8A9E) may be
encoded as follows:

+ZeVnLIqe-
温馨提示:内容为网友见解,仅供参考
第1个回答  2006-03-05
UTF7
用于处理普通 ASCII 字符 , 主要普通 ASCII 的二进制编码最高位一般为 0. 在 UTF7 下处理 ASCII 字符的时候需要处理补位 . 补位规则 : 将后一字节的从尾取位补在前一字节的头 , 补满 8 位 .
UTF8
主要用于处理图片和铃声

关于UTF7 和 UTF8编码的问题
UTF8其实和Unicode是同类,就是在编码方式上不同!首先UTF8编码后的大小是不一定,不像Unicode编码后的大小是一样的!我们先来看Unicode的编码:一个英文字母 “a” 和 一个汉字 “好”,编码后都是占用的空间大小是一样的,都是两个字节!而UTF8编码:一个英文字母“a” 和 一个汉字 “好”,编码后占用的空间大...

C#里面 Encoding 这个类怎么使用?
1、ASCIIEncoding 将 Unicode 字符编码为单个 7 位 ASCII 字符。此编码仅支持 U+0000 和 U+007F 之间的字符值。代码页 20127。还可通过 ASCII 属性获得。2、UTF7Encoding 使用 UTF-7 编码对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值。代码页 65000。还可通过 UTF7 属性获得。3、UTF...

如何解决网页上的乱码?
98”:可真正的同屏显示不同内码,即GB码和BIG5码两者共存、都能正常显示。它支持GB、HZ、BIG5、JIS、EUC、SJIS、KSC、UTF7和UTF8等格式;支持Netscape Communicator 4.X、Internet Explorer 和Office 等软件;支持在多个窗口中同时查看不同内码的文档的超级多内码显示平台。下载地址:http:\/\/ www.itw...

开启网页乱码
2、右键网页--编码--unicode(UTF-8)3、出现这种问题一般是某个傻子在无聊的时候乱点滑鼠造成的! 开启网页容易出现乱码是怎么回事 在页面的空白处右击滑鼠,选择编码(E),再勾选自动选择。。重新整理一下就好,希望能帮到你~~~ 开启网页容易出现乱码是怎么回事 乱码指的是计算机系统不能显示正确的字元,而显示其...

UltraEdit打开Exe文件,每个字符中间有个小数点,什么意思?
你的第一个程 序存这段码的时候 用的是UTF-8 或者 unicode-8编码,这程编码在存英文的时候是以00+ASC码存储的(A=00 65),也就是说,一个英文字符占了两个字节的位置。那个二进制的00是没有用的,而在UE里显示就是“.”,你用第二个程 序修改的时候,(不清楚你做的什么修改。)应该...

如何消除电脑乱码?
它支持GB、HZ、BIG5、JIS、EUC、SJIS、KSC、 UTF7和UTF8等格式;支持Netscape Communicator 4.X、Internet Explorer 和Office 等软件;支持在多个窗口中同时查看不同内码的文档的超级多内码显示平台。网页无乱码保存的方法是:用浏览器打开网页时,在“查看”\/“编码”中选择“自动选择”,存盘时保存类型选“web页”,...

请问你那个关于C#接收数组显示误码的问题解决了嘛我也是接收到的数组是...
常用的有 using System.Text;byte[] buffer = …… \/\/接收的字节数组。count为实际接收的字节数量\/\/ASCII解码string s= Encoding.ASCII.GetString(buffer, 0, count);\/\/UTF8解码string s = Encoding.UTF8.GetString(buffer, 0, count);此外,还有Encoding.UTF7、Encoding.Unicode……

MultiByteToWideChar注意
特别地,Windows CE环境中,MultiByteToWideChar函数不支持CodePage参数中的CP_UTF7和CP_UTF8值,以及wcFlags参数中的WC_NO_BEST_FIT_CHARS标志。这些限制适用于Windows CE 1.0及更高版本,以及Windows NT 3.1、Windows 95及更高版本的系统。在使用此函数时,推荐引用以下头文件:winnls.h,以及需要...

WideCharToMultiByte相关变量
如果一样,函数将失败,GetLastError将返回ERROR_INVALID_PARAMETER的值。Windows CE:不支持参数CodePage中的CP_UTF7和CP_UTF8的值,以及参数dwFlags中的WC_NO_BEST_FIT_CHARS值。速查 Windows NT 3.1、Windows 95以上、Windows CE 1.0以上,头文件:winnls.h;库文件:kernel32.lib。

VB中如何生成编码格式为UTF-8的文本文件?
' CP_UTF7 UTF-7,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL ' CP_UTF8 UTF-8,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL '第二个参数:指定如何处理没有转换的字符,但不设此参数函数会运行的更快一些,我都是把它设为0。'第三个参数: 待转换的宽字符串?

相似回答