[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bitstuct and binary files

Rob Lee

10/24/2006 6:06:00 PM

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-...t...) 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

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

14 Answers

Joel VanderWerf

10/24/2006 6:22:00 PM

0

Rob Lee wrote:
> puts f.read(3).unpack('H6').first.to_i(16)

I think you want 'h' instead of 'H', and you need to reverse the nibbles:

puts f.read(3).unpack('h6').first.reverse.to_i(16)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Ben Nagy

10/24/2006 6:37:00 PM

0

> -----Original Message-----
[...]
> Can anybody tell me why setting an audio.alength>=1024 breaks
> the output
> and provide a way to fix it ...

---borked---
num=359447
f.write(num.to_s(16)[0..2].instance_eval {(self.reverse + '0' * (6 -
self.length)).reverse}.scan(/../).inject('') {|s,byte| s <<
byte.to_i(16)})
---

My code is broken. Where I said [0..2] I meant [0..5], I added it in at the
last minute as an overflow check without testing - I was thinking 3 raw hex
bytes but the string after the conversion is a 6 byte 'friendly' hex string.

Sorry. :(

ben


Rob Lee

10/24/2006 6:44:00 PM

0

Joel VanderWerf wrote:
> Rob Lee wrote:
>> puts f.read(3).unpack('H6').first.to_i(16)
>
> I think you want 'h' instead of 'H', and you need to reverse the
> nibbles:
>
> puts f.read(3).unpack('h6').first.reverse.to_i(16)

Ah, OK that works with my example script - thanks. However, that means
the Bitstruct definition I gave is incorrect. The binary file I'm
trying to modify correctly unpacks the integer using :

f.read(3).unpack('H6').first.to_i(16)

However, I'm really confused over what the Bitstruct definition should
be to support this - do you have any suggestions ?

Thanks

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

Rob Lee

10/24/2006 6:51:00 PM

0


>
> My code is broken. Where I said [0..2] I meant [0..5], I added it in at
> the
> last minute as an overflow check without testing - I was thinking 3 raw
> hex
> bytes but the string after the conversion is a 6 byte 'friendly' hex
> string.
>
> Sorry. :(
>
> ben

No problem ! I've just modified the code and it works great - thanks :)
I'm investigating bitstruct as well but it's always nice to have
alternatives ...

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

Joel VanderWerf

10/24/2006 6:54:00 PM

0

Rob Lee wrote:
> Joel VanderWerf wrote:
>> Rob Lee wrote:
>>> puts f.read(3).unpack('H6').first.to_i(16)
>> I think you want 'h' instead of 'H', and you need to reverse the
>> nibbles:
>>
>> puts f.read(3).unpack('h6').first.reverse.to_i(16)
>
> Ah, OK that works with my example script - thanks. However, that means
> the Bitstruct definition I gave is incorrect. The binary file I'm
> trying to modify correctly unpacks the integer using :
>
> f.read(3).unpack('H6').first.to_i(16)
>
> However, I'm really confused over what the Bitstruct definition should
> be to support this - do you have any suggestions ?
>
> Thanks
>

Then probably your data is in big endian order--I should have grokked
that from your code. Try this:

class Audio < BitStruct
signed :aheader, 8
unsigned :alength, 3*8
signed :afooter, 8
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Rob Lee

10/24/2006 7:02:00 PM

0

>
> Then probably your data is in big endian order--I should have grokked
> that from your code. Try this:
>
> class Audio < BitStruct
> signed :aheader, 8
> unsigned :alength, 3*8
> signed :afooter, 8
> end

Excellent - thank you. It all works - if you're interested I'm working
on a Ruby proxy for the Nabaztag (wifi bunny rabbit) and I'm trying to
extend some of it's audio capabilities :)

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

Dr. Zachary Smith

9/29/2010 7:41:00 PM

0

On Sep 29, 1:02 pm, djhe...@kithrup.com (Dorothy J Heydt) wrote:
> In article <4cc32c1e-6638-4349-8b9b-f2d4af34f...@l25g2000prn.googlegroups..com>,
>
> Duggy  <Paul.Dug...@jcu.edu.au> wrote:
> >On Sep 29, 2:11 pm, "J.Pascal" <ju...@pascal.org> wrote:
> >> Inego, in "Princess Bride" bore scars on his cheeks.
>
> >Indigo Montoya.
>
> In~igo, actually, unless you were being funny.  The tilde should
> be over the n, but my system won't do fancy stuff like that.

Iñigo Like that? Try Alt+0241

Ric Locke

9/29/2010 9:19:00 PM

0

On Wed, 29 Sep 2010 12:40:46 -0700 (PDT), Dr. Zachary Smith wrote:

> On Sep 29, 1:02?pm, djhe...@kithrup.com (Dorothy J Heydt) wrote:
>> In article <4cc32c1e-6638-4349-8b9b-f2d4af34f...@l25g2000prn.googlegroups.com>,
>>
>> Duggy ?<Paul.Dug...@jcu.edu.au> wrote:
>>>On Sep 29, 2:11?pm, "J.Pascal" <ju...@pascal.org> wrote:
>>>> Inego, in "Princess Bride" bore scars on his cheeks.
>>
>>>Indigo Montoya.
>>
>> In~igo, actually, unless you were being funny. ?The tilde should
>> be over the n, but my system won't do fancy stuff like that.
>
> I?igo Like that? Try Alt+0241

Or the basic Windows ones: Alt-164 ? and Alt-165 ?

Regards,
Ric

J.Pascal

9/30/2010 1:56:00 AM

0

On Sep 29, 1:57 am, JF <jul...@oopsoopsfloodsclimbers.co.uk> wrote:
> J.Pascal wrote:
>
> > Which is too bad, really, because symbolism is powerful and discussing
> > how to use it effectively might actually be useful to people
> > interested in improving their craft.
>   []
>
> > In any case...  if we're in the business of pulling people's strings,
> > physical marks and markers is one way to pull them.
>
> Yes.
>
> Your email is bouncing.
>
> JF

Oh... I'm sure I know why. Try again tomorrow.

-Julie

Dan Goodman

9/30/2010 3:52:00 AM

0

Dr. Zachary Smith wrote:

> On Sep 29, 1:02?pm, djhe...@kithrup.com (Dorothy J Heydt) wrote:
> > In article
> > <4cc32c1e-6638-4349-8b9b-f2d4af34f...@l25g2000prn.googlegroups.com
> > >,
> >
> > Duggy ?<Paul.Dug...@jcu.edu.au> wrote:
> > >On Sep 29, 2:11?pm, "J.Pascal" <ju...@pascal.org> wrote:
> > >> Inego, in "Princess Bride" bore scars on his cheeks.
> >
> > > Indigo Montoya.
> >
> > In~igo, actually, unless you were being funny. ?The tilde should
> > be over the n, but my system won't do fancy stuff like that.
>
> I?igo Like that? Try Alt+0241

For this and others, I use:
AllChars
Company: -- Author: Jeroen Laarhoven
(Freeware)
Windows OS: Windows 95/98/ME/NT4/2000/XP
Languages: English; French (user manuals available in Russian and
French)
Description: AllChars is a tool that makes non-keyboard characters
(accents, typos, currency symbols, etc.) easily accessible within any
Windows application. Just type the Ctrl-key followed by two
characters that in combination specify the character. AllChars also
supports macros. You can type short names, resulting in AllChars
sending long text bits to your application. Support for macros
include secure macros for logins, insert date/time strings and more.
NOTES: If you have problems with v3.6.2, install the v3.6.3 patch.
Home page:
http://allchars.zw...
download page: v 3.6.2 [ allchars362setup.exe (474 KB)] [
allchars362.zip (361 KB)]
http://allchars.zw...download.html
download: [ allchars363patch.zip Patch for v3.6.2: ]
http://allchars.zw...download/allchars363patch.zip

--
Dan Goodman
"I have always depended on the kindness of stranglers."
Tennessee Williams, A Streetcar Named Expire
Journal dsgood.dreamwidth.org (livejournal.com, insanejournal.com)