Write string to a stream using utf16 encoding

2019-06-02 23:46发布

My code:

        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 ();
        }

It writes string to a stream, but it writes using UTF8 encoding. How to it with UTF16?

1条回答
Rolldiameter
2楼-- · 2019-06-02 23:56
System.Text.Encoding encoding = System.Text.Encoding.Unicode; 
BinaryWriter bw = new BinaryWriter (pStream, encoding);

See here.

查看更多
登录 后发表回答