David Liao

为家人活得更好而奋斗!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

怎样将string转化为 把每个char用2个byte表示?而不是一个byte

c#中 怎样将string转化为 把每个char用2个byte表示?而不是一个byte
byte[] sender = System.Text.Encoding.ASCII.GetBytes("abcd".ToCharArray()) ;
结果为sender中为{97,98,99,100}
但是要是将"abcd"改为中文,比如"你好"
结果就是{63,63}.每个汉字只用了一个byte表示,显然不正确.
怎样才能将每个汉字用两个字节表示.
-------------------
byte[] s = System.Text.Encoding.Unicode.GetBytes("你好".ToCharArray()) ;
此时s长度为4,为(96,79,125,98)
ASCII是美国标准信息交换码,自然处理不了汉字,可能把你好当作"A",采用Unicode编码就可以。
 
byte[] b=System.Text.Encoding.GetEncoding("gb2312").GetBytes("你好");
posted on 2007-02-28 09:45  David Liao  阅读(657)  评论(0)    收藏  举报