用System.IO.BinaryWriter编写字符串与char数组的区别 [英] Difference in writing string vs. char array with System.IO.BinaryWriter
问题描述
using(BinaryWriter bw = new BinaryWriter(File.Open(data.dat),Encoding。 ASCII))
{
string value =Foo;
//写入4个字节
bw.Write(value);
//写入3个字节
bw.Write(value.ToCharArray());
}
我不明白为什么当我只写3个ASCII字符。谁能解释这个?
BinaryWriter.Write(string)
表示它将一个长度为前缀的字符串写入此流。 Write(char [])$ c的超载$ c>
没有这样的前缀。
在我看来,额外的数据是长度。
编辑:
只是有点更明确,使用反射器。你会看到它在这里有这段代码,作为 Write(string)
方法的一部分:
this.Write7BitEncodedInt(BYTECOUNT);
这是一种使用尽可能少的字节对整数进行编码的方式。对于短字符串(我们每天使用的字符少于128个字符),可以使用一个字节来表示。对于较长的字符串,它开始使用更多字节。
以下是该函数的代码,以防您感兴趣:
protected void Write7BitEncodedInt(int value)
{
uint num =(uint)value;
while(num> = 0x80)
{
this.Write((byte)(num | 0x80));
num = num>> 7;
}
this.Write((byte)num);
}
使用此编码对长度进行前缀后,会为字符写入字节在所需的编码中。
I’m writing text to a binary file in C# and see a difference in quantity written between writing a string and a character array. I’m using System.IO.BinaryWriter and watching BinaryWriter.BaseStream.Length as the writes occur. These are my results:
using(BinaryWriter bw = new BinaryWriter(File.Open("data.dat"), Encoding.ASCII))
{
string value = "Foo";
// Writes 4 bytes
bw.Write(value);
// Writes 3 bytes
bw.Write(value.ToCharArray());
}
I don’t understand why the string overload writes 4 bytes when I’m writing only 3 ASCII characters. Can anyone explain this?
The documentation for BinaryWriter.Write(string)
states that it writes a length-prefixed string to this stream. The overload for Write(char[])
has no such prefixing.
It would seem to me that the extra data is the length.
EDIT:
Just to be a bit more explicit, use Reflector. You will see that it has this piece of code in there as part of the Write(string)
method:
this.Write7BitEncodedInt(byteCount);
It is a way to encode an integer using the least possible number of bytes. For short strings (that we would use day to day that are less than 128 characters), it can be represented using one byte. For longer strings, it starts to use more bytes.
Here is the code for that function just in case you are interested:
protected void Write7BitEncodedInt(int value)
{
uint num = (uint) value;
while (num >= 0x80)
{
this.Write((byte) (num | 0x80));
num = num >> 7;
}
this.Write((byte) num);
}
After prefixing the the length using this encoding, it writes the bytes for the characters in the desired encoding.
这篇关于用System.IO.BinaryWriter编写字符串与char数组的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!