# Ruby and byte

**URL:** <https://rubytalk.org/t/ruby-and-byte/28877>\
**Category:** ruby-talk\
**Created:** [12 July 2006 09:28 UTC](https://rubytalk.org/t/ruby-and-byte/28877 "2006-07-12T09:28:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agnieszka\_Figiel](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Agnieszka\_Figiel](https://rubytalk.org/u/Agnieszka_Figiel)\
**Post date:** [12 July 2006 09:28 UTC](https://rubytalk.org/t/ruby-and-byte/28877/1 "2006-07-12T09:28:41Z")

</div>

Hello!

How would you go about representing the byte type in Ruby? I have a task  
where I neeed to be writing and reading bytes from a tcp socket - so for  
instance I might want to send/receive an integer which is 1 byte long,  
other times 4 bytes. in Java you can do that easily with the byte type,  
so far I have no good idea how to do this in Ruby.

> **···**
>
> --  
> Agnieszka
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pedro\_Corte-Real1](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Pedro\_Corte-Real1](https://rubytalk.org/u/Pedro_Corte-Real1)\
**Post date:** [12 July 2006 09:49 UTC](https://rubytalk.org/t/ruby-and-byte/28877/2 "2006-07-12T09:49:57Z")

</div>

Use a String.

Pedro.

> **···**
>
> On 7/12/06, Agnieszka Figiel \<agnieszka.figiel@gmail.com\> wrote:
> 
> > How would you go about representing the byte type in Ruby? I have a task  
> > where I neeed to be writing and reading bytes from a tcp socket - so for  
> > instance I might want to send/receive an integer which is 1 byte long,  
> > other times 4 bytes. in Java you can do that easily with the byte type,  
> > so far I have no good idea how to do this in Ruby.

---

<div class="post-metadata">

**Author:** ![Carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Carlos](https://rubytalk.org/u/Carlos)\
**Post date:** [12 July 2006 10:49 UTC](https://rubytalk.org/t/ruby-and-byte/28877/3 "2006-07-12T10:49:13Z")

</div>

Agnieszka Figiel wrote:

> Hello!
> 
> How would you go about representing the byte type in Ruby? I have a task where I neeed to be writing and reading bytes from a tcp socket - so for instance I might want to send/receive an integer which is 1 byte long, other times 4 bytes. in Java you can do that easily with the byte type, so far I have no good idea how to do this in Ruby.

Use Array#pack to put your bytes/integers/etc in a string. Send and receive strings. Use String#unpack to extract bytes/integers/etc from the string.

Good luck.

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://rubytalk.org/u/Joey)\
**Post date:** [12 July 2006 10:50 UTC](https://rubytalk.org/t/ruby-and-byte/28877/4 "2006-07-12T10:50:09Z")

</div>

class Integer  
&nbsp;&nbsp;def bytes  
&nbsp;&nbsp;&nbsp;&nbsp;" "\*self  
&nbsp;&nbsp;end  
end

send(10.bytes)

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

> **···**
>
> On 7/12/06, Pedro Côrte-Real \<pedrocr@gmail.com\> wrote:
> 
> > On 7/12/06, Agnieszka Figiel \<agnieszka.figiel@gmail.com\> wrote:  
> > \> How would you go about representing the byte type in Ruby? I have a task  
> > \> where I neeed to be writing and reading bytes from a tcp socket - so for  
> > \> instance I might want to send/receive an integer which is 1 byte long,  
> > \> other times 4 bytes. in Java you can do that easily with the byte type,  
> > \> so far I have no good idea how to do this in Ruby.
> > 
> > Use a String.
> > 
> > Pedro.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [12 July 2006 20:20 UTC](https://rubytalk.org/t/ruby-and-byte/28877/5 "2006-07-12T20:20:48Z")

</div>

Carlos wrote:

> Agnieszka Figiel wrote:
> 
> > Hello!
> > 
> > How would you go about representing the byte type in Ruby? I have a task where I neeed to be writing and reading bytes from a tcp socket - so for instance I might want to send/receive an integer which is 1 byte long, other times 4 bytes. in Java you can do that easily with the byte type, so far I have no good idea how to do this in Ruby.
> 
> Use Array#pack to put your bytes/integers/etc in a string. Send and receive strings. Use String#unpack to extract bytes/integers/etc from the string.

And you might want to check out bit-struct for a more object-oriented and readable wrapper around pack and unpack:

[http://redshift.sourceforge.net/bit-struct/](http://redshift.sourceforge.net/bit-struct/)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
