今天在工作中遇到一个问题,如何将一个string类型转成byte[]?

  这种问题就属于知道了就及其简单,不知道就够你忙活半天的那种。其实只要一个语句就OK了:

  
byte[] byteArray = System.Text.Encoding.Default.GetBytes(  str  );

  怎么样,够简单吧?

  反过来也是一样,把byte[]转成string:

string str = System.Text.Encoding.Default.GetString( byteArray );

  其实,在System.Text.Encoding class中,还有很多其它有用的方法,像GetChars,从一个byte[]转成一个char[],等等,可以参考MSDN。

  另外,还有其它编码方式的,如System.Text.UTF8Encoding class、System.Text.UnicodeEncoding class等,根据不同需要可选不同的class。
posted on 2007-08-08 10:04  flyluo  阅读(1199)  评论(0编辑  收藏  举报