[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String.unpack endianess issue?

matthew.moss.coder

1/26/2006 6:22:00 AM

In irb 0.9 (ruby 1.8.2) in cygwin/WindowsXP:

> a = [0, 0, 0, 6]
=> [0, 0, 0, 6]
> x = a.pack("C*")
=> "\000\000\000\006"
> x.unpack("I")
=> [100663296]
> x.unpack("V")
=> [100663296]


I would have though that at least one of "I" or "V" in the unpack call
would have given me the correct answer, 6. I'm reading this data out
of a binary file, stored in big-endian, but neither handles this.

I managed to get the right answer by doing:

x.unpack("C*").reverse.pack("C*").unpack("I")

But that seems rather silly. So I guess I'm asking:

1. Is unpack broken, or is there a big-endian option I missed?
2. Is there a more generic module/library for reading binary files?
3. Is there an existing modules/lib for reading MIDI files?

thanks...


4 Answers

Joel VanderWerf

1/26/2006 6:44:00 AM

0

Matthew Moss wrote:
> In irb 0.9 (ruby 1.8.2) in cygwin/WindowsXP:
>
>> a = [0, 0, 0, 6]
> => [0, 0, 0, 6]
>> x = a.pack("C*")
> => "\000\000\000\006"
>> x.unpack("I")
> => [100663296]
>> x.unpack("V")
> => [100663296]
>
>
> I would have though that at least one of "I" or "V" in the unpack call
> would have given me the correct answer, 6. I'm reading this data out
> of a binary file, stored in big-endian, but neither handles this.
>
> I managed to get the right answer by doing:
>
> x.unpack("C*").reverse.pack("C*").unpack("I")
>
> But that seems rather silly. So I guess I'm asking:
>
> 1. Is unpack broken, or is there a big-endian option I missed?
> 2. Is there a more generic module/library for reading binary files?
> 3. Is there an existing modules/lib for reading MIDI files?
>
> thanks...
>

irb(main):001:0> a = [0, 0, 0, 6]
=> [0, 0, 0, 6]
irb(main):002:0> x = a.pack("C*")
=> "\000\000\000\006"
irb(main):003:0> x.unpack "N"
=> [6]

For convenience, I wrote a set of object-oriented wrappers around
pack/unpack. See bit-struct on raa.

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


matthew.moss.coder

1/26/2006 2:35:00 PM

0

> irb(main):001:0> a = [0, 0, 0, 6]
> => [0, 0, 0, 6]
> irb(main):002:0> x = a.pack("C*")
> => "\000\000\000\006"
> irb(main):003:0> x.unpack "N"
> => [6]
>
> For convenience, I wrote a set of object-oriented wrappers around
> pack/unpack. See bit-struct on raa.

Ah, thanks. I noticed the "V" as I searched for "endian" in the docs,
but I missed the "N". Should have searched on "byte order". Thanks...


Albert, Archi-druide français

4/12/2013 7:40:00 AM

0

Le 12/04/2013 09:29, poilagratter a ??crit :
> "Albert, Archi-druide fran??ais" <albert@france> wrote:
>> Le 11/04/2013 13:23, poilagratter a ??crit :
>>> "Albert, Archi-druide fran??ais" <albert@france> wrote:
>>>> Le 10/04/2013 12:26, poilagratter a ??crit :
>>>>> "Albert, Archi-druide fran??ais" <albert@france> wrote:
>>>>>>
>>>>>>
>>>>>> Ici je vois la sauvegarde de 3.400 emplois, finalement le b??n??fice dans
>>>>>> cette affaire c'est clairement la France. Ce baraki oublie un peu trop
>>>>>> rapidement qu'il habite une r??gion qui ne garde la t??te hors de l'eau que
>>>>>> gr??ce au fond structurel europ??en et ?? l'argent venant de Flandre....
>>>>>
>>>>>
>>>>> 3400 emplois pour Boukaquistanais.
>>>>>
>>>>
>>>> L'H??tellerie Restauration des m??tiers pour "Boukaquistanais" !
>>>>
>>>> Quel m??pris vous avez pour des gens qui pratiquent ces professions
>>>> ??prouvantes. Verriez vous une objection ?? tenir les m??mes propos au
>>>> milieu d'un restaurant....je viendrais filmer..
>>>>
>>>
>>>
>>> Pousse la porte des cuisines et va voir qui travaille aux fourneaux, a la
>>> plonge etc.
>>> Va dans les grands h??tels voir qui fait les chambres, qui sert les p'tits
>>> d??j et qui est en salle pour les banquets.
>>>
>>
>> Et apr??s ? Qu'est ce qui vous chiffonne ici ? Ils travaillent, je ne vois
>> pas ou est le probl??me..
>
>
> Et toi tu travailles du chapeau.

>
C'est le mot "travail" qui vous g??ne ?






--
Envoutement/d??senvoutement, retour d'affection, allongement du p??nis,
exorcisme,
r??pulsif anti franchouillards, filtre magique contre la cirrhose du foie
etc..

Fustigator

4/12/2013 10:28:00 AM

0

Vitae forma vocata poilagratter <address_is@invalid.invalid> , die
Fri, 12 Apr 2013 07:29:04 +0000 (UTC), in littera
<kk8d40$e40$2@speranza.aioe.org> in foro be.politics(et aliis) vere
scripsit quod sequitur:

>"Albert, Archi-druide fran?ais" <albert@france> wrote:
>> Le 11/04/2013 13:23, poilagratter a ?crit :
>>> "Albert, Archi-druide fran?ais" <albert@france> wrote:
>>>> Le 10/04/2013 12:26, poilagratter a ?crit :
>>>>> "Albert, Archi-druide fran?ais" <albert@france> wrote:
>>>>>>
>>>>>>
>>>>>> Ici je vois la sauvegarde de 3.400 emplois, finalement le b?n?fice dans
>>>>>> cette affaire c'est clairement la France. Ce baraki oublie un peu trop
>>>>>> rapidement qu'il habite une r?gion qui ne garde la t?te hors de l'eau que
>>>>>> gr?ce au fond structurel europ?en et ? l'argent venant de Flandre....
>>>>>
>>>>>
>>>>> 3400 emplois pour Boukaquistanais.
>>>>>
>>>>
>>>> L'H?tellerie Restauration des m?tiers pour "Boukaquistanais" !
>>>>
>>>> Quel m?pris vous avez pour des gens qui pratiquent ces professions
>>>> ?prouvantes. Verriez vous une objection ? tenir les m?mes propos au
>>>> milieu d'un restaurant....je viendrais filmer..
>>>>
>>>
>>>
>>> Pousse la porte des cuisines et va voir qui travaille aux fourneaux, a la
>>> plonge etc.
>>> Va dans les grands h?tels voir qui fait les chambres, qui sert les p'tits
>>> d?j et qui est en salle pour les banquets.
>>>
>>
>> Et apr?s ? Qu'est ce qui vous chiffonne ici ? Ils travaillent, je ne vois
>> pas ou est le probl?me..
>
>
>Et toi tu travailles du chapeau.

...et de la serpe d'or pour cueillir le gui...
--

Fusti