Question

If I try to write a packed byte as an unsigned int32 to a file, using pack('L'), the number 10 writes as five bytes. With any another number I get the normal behavior.

I wrote simple script:

x = 0
while x < 100 do
    puts "\nTesting %s..." % [x]
    h = [x].pack('L')
    puts "h length: %s" % [h.length]
    f = File.open('tmp', 'w')
    f.write h
    puts "position: %s" % [f.pos]
    x += 1
end

and get this result; Look at iteration number 10.

Where is my mistake?

Ruby version: ruby 1.9.3p327 (2012-11-10) [i386-mingw32]

Was it helpful?

Solution

It's a newline translation issue (Windows-specific): byte 10 is translated to CR+LF.

I don't know ruby, but open with wb instead of w might help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top