R
Rob Lee
Hi,
I've been working with modifying a binary file to replace some meta-data
included within. I've had some discussions previously
(http://www.ruby-forum.com/topic/85591) about this but couldn't get to a
satisfactory conclusion. Basically I need to encode an integer value
into 3 bytes as part of a binary file - Bitstruct was suggested as one
of the solutions, however I haven't been able to get it to work for
values larger than 1024 :
require 'bit-struct'
class Audio < BitStruct
signed :aheader, 8, :endian => :little
unsigned :alength, 3*8, :endian => :little
signed :afooter, 8, :endian => :little
end
audio = Audio.new
audio.aheader = 1
audio.alength = 3027
audio.afooter = 1
f = open("testfile","wb")
f.write(audio)
f.close
f = open("testfile","rb")
f.pos=0
puts f.read(1).unpack("c").first.to_s
puts f.read(3).unpack('H6').first.to_i(16)
puts f.read(1).unpack("c").first.to_s
f.close
In the application above, if the value of audio.alength is set to equal
or below 1024 then the output for f.read(3).unpack('H6').first.to_i(16)
matches, however if set above this then the value printed doesn't match.
Can anybody tell me why setting an audio.alength>=1024 breaks the output
and provide a way to fix it ...
Thanks
I've been working with modifying a binary file to replace some meta-data
included within. I've had some discussions previously
(http://www.ruby-forum.com/topic/85591) about this but couldn't get to a
satisfactory conclusion. Basically I need to encode an integer value
into 3 bytes as part of a binary file - Bitstruct was suggested as one
of the solutions, however I haven't been able to get it to work for
values larger than 1024 :
require 'bit-struct'
class Audio < BitStruct
signed :aheader, 8, :endian => :little
unsigned :alength, 3*8, :endian => :little
signed :afooter, 8, :endian => :little
end
audio = Audio.new
audio.aheader = 1
audio.alength = 3027
audio.afooter = 1
f = open("testfile","wb")
f.write(audio)
f.close
f = open("testfile","rb")
f.pos=0
puts f.read(1).unpack("c").first.to_s
puts f.read(3).unpack('H6').first.to_i(16)
puts f.read(1).unpack("c").first.to_s
f.close
In the application above, if the value of audio.alength is set to equal
or below 1024 then the output for f.read(3).unpack('H6').first.to_i(16)
matches, however if set above this then the value printed doesn't match.
Can anybody tell me why setting an audio.alength>=1024 breaks the output
and provide a way to fix it ...
Thanks