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

标签 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?

最佳答案

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


See here.

关于c# - 使用utf16编码将字符串写入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610036/

相关文章:

c# - 在 Unity 中注册时是否可以指定可变长度的构造函数参数?

c# - 使用字符串格式格式化小数

ruby - 根据长度获取数组元素

php - MySQL PHP - 如何显示存储在数据库中的不可读字符

c# - 在 C# 中避免多个 if 语句

c# - Linq to XML 选择所有父项和子项

iphone - String to CLLocation经纬度

java - 检查匹配的标签

c - GLib 将 GString 从 UTF-8 转换为 UTF-16/32 可能吗?

php - 如何避免在php中回显字符65279?