# Could anyone help me in the question, thanks?

**URL:** <https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725>\
**Category:** Ruby\
**Created:** [June 25, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725 "2007-06-25T20:02:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [June 25, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725/1 "2007-06-25T20:02:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [June 25, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725/2 "2007-06-25T20:37:24Z")

</div>

Sonn Y. 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/](http://raa.ruby-lang.org/project/bit-struct/) if your needs are more  
complicated.

---

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [June 25, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725/3 "2007-06-25T20:47:52Z")

</div>

Alex Y. wrote:

> Sonn Y. 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/](http://raa.ruby-lang.org/project/bit-struct/) if your needs are more  
> complicated.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![rubyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rubyer](https://www.ruby-forum.com/u/rubyer)\
**Post date:** [June 25, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/could-anyone-help-me-in-the-question-thanks/107725/4 "2007-06-25T20:55:28Z")

</div>

Sonn Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
