# Sending data types in a UDP packet

**URL:** <https://www.ruby-forum.com/t/sending-data-types-in-a-udp-packet/167093>\
**Category:** Ruby\
**Created:** [May 3, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/sending-data-types-in-a-udp-packet/167093 "2009-05-03T20:19:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/41988e/32.png) [@ibw](https://www.ruby-forum.com/u/ibw)\
**Post date:** [May 3, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/sending-data-types-in-a-udp-packet/167093/1 "2009-05-03T20:19:32Z")

</div>

In the specific protocol I’m writing a script for, I need to send a lot  
of different data types through the packet, such as U32, and S8.  
(Unsigned 32 bit, signed 8 bit). Does ruby support these types of  
variables? How can I send them through a packet?  
I’m still learning about networking and ruby, so if I said anything that  
doesn’t make sense, please forgive me.

---

<div class="post-metadata">

**Author:** ![ibw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/41988e/32.png) [@ibw](https://www.ruby-forum.com/u/ibw)\
**Post date:** [May 3, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/sending-data-types-in-a-udp-packet/167093/2 "2009-05-03T20:55:25Z")

</div>

Isaac W. wrote:

> In the specific protocol I’m writing a script for, I need to send a lot  
> of different data types through the packet, such as U32, and S8.  
> (Unsigned 32 bit, signed 8 bit). Does ruby support these types of  
> variables? How can I send them through a packet?  
> I’m still learning about networking and ruby, so if I said anything that  
> doesn’t make sense, please forgive me.

The basic way of constructing packets in ruby is with Array#pack, and  
String#unpack on the other end. See the ri docs or the pickaxe book, or  
ask back here for more help.

There are some libraries that give you a little language for describing  
and manipulating packets, which can simplify your code and make it  
easier to maintain. I wrote bit-struct[1] and there are at least two  
others[2][3].

[1] [http://raa.ruby-lang.org/project/bit-struct](http://raa.ruby-lang.org/project/bit-struct)  
[2] [http://rubyforge.org/projects/bindata](http://rubyforge.org/projects/bindata)  
[3] [http://metafuzz.rubyforge.org/binstruct](http://metafuzz.rubyforge.org/binstruct)
