我错过了什么?在什么世界中需要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. Theseare boolean values. I need there to be 8 bits in every byte. I know Icould write these bits out as char''s using one bit per byte, but thatwould 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 bytesper boolean value.What am I missing? In what world does it take 4 bytes to represent aboolean value? Is there some sort of alignment issue that''s causingthis?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 itsounds like you want.StreamWriter.Write(bool) writes out the *text* representation of aboolean, as documented.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me tooTakes 4 bytes since that is the memory layout of a bool. Based on the waythe streams work, the best they could do is 1 byte per bool if you usedWrite(bool) Read(bool). However, you could do better by having a set ofmethods that took an array of bools and converted that into a series of bytesdepending on how many bools you were emitting.--Justin RogersDigiTec Web Consultants, LLC.Blog: http://weblogs.asp.net/justin_rogers"John" <jo********@hotmail.com> wrote in messagenews: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!!!! JohnJustin 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 5bytes if you passed in false (assuming an encoding which uses one byteper character, of course).--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 这篇关于StreamWriter.Write(bool值)---文件中应该有多少位/字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!