Forum: Ruby could anyone help me in the question, thanks?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E322f689fb9e4bd6fc035d0b54859612?d=identicon&s=25 Sonn Ygg (rubyer)
on 2007-06-25 20:02
In network application, we need to fill some fields of protocol header
like below (in C):

struct hdr {
     unsinged short ver;
     unsinged char  len;
     unsinged char  flg;
     ......
};

int main(void)
{
    struct hdr hh;
    hh.ver = 0x1;
    hh.len = 0x2;
    hh.flg = 0x3;
    ...
    send(sock, &hh, sizeof(hh), 0);
    ...
}

How could I do above in Ruby because I don't find how to specify the
data type of variables or the address of variables in Ruby so far? I
will deeply appreciate if anyone gives me any hint about it? Thanks a
lot.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-25 20:37
(Received via mailing list)
Sonn Ygg wrote:
> int main(void)
> How could I do above in Ruby because I don't find how to specify the
> data type of variables or the address of variables in Ruby so far? I
> will deeply appreciate if anyone gives me any hint about it? Thanks a
> lot.
>
You only need to worry about the types at the
serialisation/deserialisation interface, and you can do that with
Array#pack and String#unpack.  There's also bitstruct at
http://raa.ruby-lang.org/project/bit-struct/ if your needs are more
complicated.
E322f689fb9e4bd6fc035d0b54859612?d=identicon&s=25 Sonn Ygg (rubyer)
on 2007-06-25 20:47
Alex Young wrote:
> Sonn Ygg wrote:
>> int main(void)
>> How could I do above in Ruby because I don't find how to specify the
>> data type of variables or the address of variables in Ruby so far? I
>> will deeply appreciate if anyone gives me any hint about it? Thanks a
>> lot.
>>
> You only need to worry about the types at the
> serialisation/deserialisation interface, and you can do that with
> Array#pack and String#unpack.  There's also bitstruct at
> http://raa.ruby-lang.org/project/bit-struct/ if your needs are more
> complicated.

Thanks a lot!
Cf6d0868b2b4c69bac3e6f265a32b6a7?d=identicon&s=25 Daniel Martin (Guest)
on 2007-06-25 20:55
(Received via mailing list)
Sonn Ygg <ys_manman@hotmail.com> writes:

> How could I do above in Ruby because I don't find how to specify the
> data type of variables or the address of variables in Ruby so far? I
> will deeply appreciate if anyone gives me any hint about it? Thanks a
> lot.

This is exactly the problem that Array.pack was designed to solve.
Quoting from a recent post of mine dealing with network headers in the
FSP protocol: (ruby-talk:256654)

  fsp_string = [fsp_pkt.cmd, 0, fsp_pkt.key,
                fsp_pkt.seq, fsp_pkt.len, fsp_pkt.pos].pack("CCnnnN")

Basically, make an array containing what you need, then use pack to
stuff it into a byte string with the proper width and alignment.  Then
write that byte string to your socket.
This topic is locked and can not be replied to.