当前位置: 移动技术网 > IT编程>开发语言>c# > 基于C#开发中的那些编码问题(详谈)

基于C#开发中的那些编码问题(详谈)

2019年07月18日  | 移动技术网IT编程  | 我要评论
最近一直在搞各种编码问题,略有心得,与大家分享一番。 system.text提供了encoding的抽象类,这个类提供字符串编码的方法。常用的编码方式主要有ascii,u

最近一直在搞各种编码问题,略有心得,与大家分享一番。

system.text提供了encoding的抽象类,这个类提供字符串编码的方法。常用的编码方式主要有ascii,unicode,utf8(unicode编码的一种)。

unicode有四种编码格式,utf-8, utf-16,utf-32,utf-7。

字符编码类,asciiencoding ,utf7encoding,unicodeencoding,utf32encoding。

下面对ascii和unicode编码进行对比,废话不说,先上代码:

这是ascii编码、解码。

static void main(string[] args)
  {
   string temp = "hello world!";
   console.writeline("original string:{0}", temp);
   
   byte[] tempbytes = system.text.encoding.ascii.getbytes(temp);
   console.writeline("bytes array:{0}", bitconverter.tostring(tempbytes));

   biginteger integer = new biginteger(tempbytes);
   console.writeline("biginteger:{0}", integer);

   string res = system.text.encoding.ascii.getstring(tempbytes);
   console.writeline("convert back string:{0}", res);
   console.readkey();
  }

运行结果如下:

original string:hello world!
bytes array:48-65-6c-6c-6f-20-57-6f-72-6c-64-21
biginteger:10334410032597741434076685640
convert back string:hello world!

很正常,对吧?但是,如果输入的字符串是中文(或者其他非ascii表中的字符),情况会怎么样呢?

改变上面的程序代码

string temp = "你好,世界!";

运行结果如下:

original string:你好,世界!
bytes array:3f-3f-3f-3f-3f-3f
biginteger:69540876599103
convert back string:??????

如果把编码格式换成utf8,依然重复上面的测试过程。

static void main(string[] args)
  {
   string temp = "你好,世界!";
   console.writeline("original string:{0}", temp);
   
   byte[] tempbytes = system.text.encoding.utf8.getbytes(temp);
   console.writeline("bytes array:{0}", bitconverter.tostring(tempbytes));

   biginteger integer = new biginteger(tempbytes);
   console.writeline("biginteger:{0}", integer);

   string res = system.text.encoding.utf8.getstring(tempbytes);
   console.writeline("convert back string:{0}", res);
   console.readkey();
  }

运行结果如下:

original string:你好,世界!
bytes array:e4-bd-a0-e5-a5-bd-ef-bc-8c-e4-b8-96-e7-95-8c-ef-bc-81
biginteger:-10998968812899434720462615123889939386679836
convert back string:你好,世界!
original string:hello world!
bytes array:48-65-6c-6c-6f-20-57-6f-72-6c-64-21
biginteger:10334410032597741434076685640
convert back string:hello world!

通过对比,我们发现除了兼容中文和其他语言外,似乎没有太大区别。如果把编码集换成unicode,中英文字符编码的不同就会很容易看出来了。

original string:hello world!
bytes array:48-00-65-00-6c-00-6c-00-6f-00-20-00-57-00-6f-00-72-00-6c-00-64-00-21-00
biginteger:3160918205608148134863399242437668999277801104545742920
convert back string:hello world!
original string:你好,世界!
bytes array:60-4f-7d-59-0c-ff-16-4e-4c-75-01-ff
biginteger:-307722159543719876182061216
convert back string:你好,世界!

如果不考虑其他情况。通过对比结果,我们发现:

1、ascii只能处理英文和英文符号,具体请参考ascii字符表

2、unicode可以处理全球所有语言符号

3、unicode处理英文时,会在每个字节后面加一个字节0x00,比ascii多出一倍的长度;处理中文时,编码较短。

4、utf8处理中文时比unicode编码长,处理英文时与ascii一样。

结论,由于现在存储介质越来越不值钱,在处理有非英文字符时,编码格式应该选择unicode(或其子集utf8等的任意一种编码格式),只有在确定程序只会处理英文的时候,才能选择ascii编码。

以上这篇基于c#开发中的那些编码问题(详谈)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网