BinaryWriter.Write()如何写入字符串 [英] How BinaryWriter.Write() write string

查看:70
本文介绍了BinaryWriter.Write()如何写入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BinaryWriter.Write()编写字符串,在msdn中,说明如下:

I used BinaryWriter.Write() to write strings, in msdn, the description is as below:

将长度前缀的字符串写入当前流中的此编码BinaryWriter,并前进流在中的当前位置根据使用的编码和所写的特定字符到流中.

Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

我认为长度前缀是固定大小的;但实际上它是可变大小的.关于此方法如何计算前缀长度的任何细节?

I thought the length-prefix's is fix-sized; but in fact it's variable-sized. Any detail on how this method calculate the prefix's length?

推荐答案

对于.NET 4.0及更高版本,MSDN 也说:

For .NET 4.0 and above, MSDN also says:

以长度为前缀的字符串表示字符串的长度,方法是在字符串的前面加上包含该字符串长度的单个字节或单词.此方法首先将字符串的长度写为UTF-7编码的无符号整数,然后使用BinaryWriter实例的当前编码将这么多字符写入流中.

A length-prefixed string represents the string length by prefixing to the string a single byte or word that contains the length of that string. This method first writes the length of the string as a UTF-7 encoded unsigned integer, and then writes that many characters to the stream by using the BinaryWriter instance's current encoding.

对于早期版本,它说:

以长度为前缀的字符串表示字符串的长度,方法是在字符串的前面加上包含该字符串长度的单个字节或单词.此方法首先将字符串的长度写为四字节的无符号整数,然后使用BinaryWriter实例的当前Encoding将这么多字符写入流中.

A length-prefixed string represents the string length by prefixing to the string a single byte or word that contains the length of that string. This method first writes the length of the string as a four-byte unsigned integer, and then writes that many characters to the stream by using the BinaryWriter instance's current Encoding.

这篇关于BinaryWriter.Write()如何写入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆