StreamWriter.Write(bool值)---文件中应该有多少位/字节? [英] StreamWriter.Write(bool value) --- How many bits/bytes should end up in the file?
问题描述
我需要写出从其他进程收到的位。这些
是布尔值。我需要每个字节有8位。我知道我
可以将这些位写成char',每个字节使用一位,但是
将是空间效率低的。
>
我正在使用od(八进制转储)查看通过调用
..Write(true)和.Write(false)生成的文件,看起来它写出来了4个字节
每个布尔值
。
我缺少什么?在什么世界中需要4个字节来表示
布尔值?是否有某种对齐问题导致
这个?
感谢!!!!
John
John< jo ******** @ hotmail.com>写道:我需要写出从另一个进程收到的位。这些是布尔值。我需要每个字节有8位。我知道我可以使用每个字节一位来将这些位写成char',但是
将是空间效率低的。
我正在使用od(八进制转储)查看通过调用
.Write(true)和.Write(false)生成的文件,它看起来像每个布尔值写出4个字节
>我错过了什么?在什么世界中需要4个字节来表示
布尔值?是否存在导致这种问题的某种对齐问题?
StreamWriters用于* text *数据,而不是二进制数据,这就是它
听起来像你想要的。
StreamWriter.Write(bool)写出一个
布尔值的* text *表示,记录下来。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复请不要给我发邮件
占用4个字节,因为这是bool的内存布局。根据流的工作原理,他们能做的最好的是每个bool 1个字节如果你使用
写(bool)Read(bool)。但是,你可以通过一组
方法做得更好,这些方法采用一系列bool并将其转换为一系列字节
,具体取决于你发出的bool数量。
-
贾斯汀罗杰斯
DigiTec网络顾问有限责任公司。
博客: http://weblogs.asp.net/justin_rogers
"约翰" <乔******** @ hotmail.com>在消息中写道
news:d8 ************************** @ posting.google.c om ...
我需要写出从其他进程收到的位。这些是布尔值。我需要每个字节有8位。我知道我可以使用每个字节一位来将这些位写成char',但是
将是空间效率低的。
我正在使用od(八进制转储)查看通过调用
.Write(true)和.Write(false)生成的文件,它看起来像每个布尔值写出4个字节
>我错过了什么?在什么世界中需要4个字节来表示
布尔值?是否存在导致这种情况的某种对齐问题?
感谢!!!!
约翰
Justin Rogers< Ju **** @ games4dotnet.com>写道:需要4个字节,因为这是bool的内存布局。
不,它需要4个字节,因为它是写出真实 - 如果传入false,则需要5
字节(假设编码使用一个字节
每个字符
当然)。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件
Hi,
I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char''s using one bit per byte, but that
would be space-inefficient.
I''m using od (octal dump) to look at a file produced by calling
..Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.
What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that''s causing
this?
THANKS!!!!
John
John <jo********@hotmail.com> wrote:I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char''s using one bit per byte, but that
would be space-inefficient.
I''m using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.
What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that''s causing
this?
StreamWriters are for *text* data, not binary data, which is what it
sounds like you want.
StreamWriter.Write(bool) writes out the *text* representation of a
boolean, as documented.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Takes 4 bytes since that is the memory layout of a bool. Based on the way
the streams work, the best they could do is 1 byte per bool if you used
Write(bool) Read(bool). However, you could do better by having a set of
methods that took an array of bools and converted that into a series of bytes
depending on how many bools you were emitting.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"John" <jo********@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...Hi,
I need to write out bits that I receive from another process. These
are boolean values. I need there to be 8 bits in every byte. I know I
could write these bits out as char''s using one bit per byte, but that
would be space-inefficient.
I''m using od (octal dump) to look at a file produced by calling
.Write(true) and .Write(false) and it looks like it writes out 4 bytes
per boolean value.
What am I missing? In what world does it take 4 bytes to represent a
boolean value? Is there some sort of alignment issue that''s causing
this?
THANKS!!!!
John
Justin Rogers <Ju****@games4dotnet.com> wrote:Takes 4 bytes since that is the memory layout of a bool.
Nope, it takes 4 bytes since it''s writing out "True" - it would take 5
bytes if you passed in false (assuming an encoding which uses one byte
per character, of course).
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于StreamWriter.Write(bool值)---文件中应该有多少位/字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!