Wu Junchen wrote:
> irb(main):001:0> f=open('test','w')
> => #<File:test>
> irb(main):002:0> f<<[65535].pack('i')
> => #<File:test>
> irb(main):003:0> f.tell
> => 4
> irb(main):004:0> f<<[720850].pack('i')
> => #<File:test>
> irb(main):005:0> f.tell
> => 9
> the integer 720850 takes 5 bytes in my file,but it should take 4 bytes 
> only!How can I fix this?Thanks!

>irb
irb(main):001:0> x = [720850].pack('i')
=> "\322\377\n\000"
irb(main):002:0> x.length
=> 4

So clearly the integer 720850 is packed into 4 bytes as requested. Why 
does it occupy 5 bytes in the file? But see the "\n" in position 2? That 
means that the 3rd byte is a newline character, and on Windows, in text 
files, Ruby turns newlines into CRLF. 2 bytes! Since you've got binary 
data in your file you don't want to write a text file, so you must open 
the file with the "b" flag in addition to "w":

f = open("test", "wb")

-- 
Posted via http://www.ruby-forum.com/.