使用utf16编码将字符串写入流

时间:2012-03-07 22:19:17

标签: c# string utf-8 stream

我的代码:

        public static void DoStrumienia (string pString, Stream pStream)
        {
              if (pStream == null) throw new ArgumentNullException ();

              BinaryWriter bw = new BinaryWriter (pStream);

              int rozmiar = pString.Length;
              bw.Write (rozmiar);

              for (int i = 0; i < rozmiar; i++)
              {
                    bw.Write (pString[i]);
              }

              bw.Flush ();
        }

它将字符串写入流,但它使用UTF8编码进行写入。 如何使用UTF16?

1 个答案:

答案 0 :(得分:11)

System.Text.Encoding encoding = System.Text.Encoding.Unicode; 
BinaryWriter bw = new BinaryWriter (pStream, encoding);

See here.