# Reading byte stream from network

**URL:** <https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220>\
**Category:** Ruby\
**Created:** [August 16, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220 "2007-08-16T14:46:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/1 "2007-08-16T14:46:59Z")

</div>

Hi

I hope this is an OK place to ask a beginner question. Right now I am  
just playing around with Ruby and IMO the best way to play around is  
actually writing smtg. So I wanted to convert one of my old C programs  
into Ruby. This simple program is listening on a UDP port and receives  
some formatted messages send by a program. Sender program is actually  
sending out a C struct. Smgt like:

typedef struct header{  
UINT32 x;  
UINT32 y;  
UINT32 z;  
} header\_type;

I came upto a point in which I created an UDPSocket and recvfrom works  
fine. But the received string is ofcourse cryptic. How can I convert  
this byte stream into something meaningful again?

First question, how can I change network to host byte order? ( like  
ntoh?)  
And then how can i put this byte stream into a struct or smtg else.  
(The real aim is getting x,y,z quickly)

Thanks in advance.

Akif,

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/2 "2007-08-16T14:49:15Z")

</div>

unknown wrote:

> typedef struct header{  
> UINT32 x;  
> UINT32 y;  
> UINT32 z;  
> } header\_type;

Take a look at unpack (ri String#unpack)

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/3 "2007-08-16T15:41:11Z")

</div>

On 16 A ustos, 15:49, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Stefan
> 
> Posted viahttp://www.ruby-forum.com/.

Thank you. Just what am I looking for.

Akif,

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/4 "2007-08-16T17:07:23Z")

</div>

On Aug 16, 8:42 am, removed\_email\_address@domain.invalid wrote:

> UINT32 x;  
> ntoh?)  
> And then how can i put this byte stream into a struct or smtg else.  
> (The real aim is getting x,y,z quickly)
> 
> Thanks in advance.
> 
> Akif,

Yeah, look at String#reverse in conjuction w/ unpack. One of the  
coolest and most confusing aspects of ruby (for me as a C programmer  
anyway) is the equivalence between strings and unstructured binary.  
It’s confusing b/c buffers of characters in C in a way have “duck  
typing” as you usually read them in as an unsigned char \*. Usually  
with OO languages other than ruby, you have to go through some madness  
to get unstructured data into a string, or vice versa.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/5 "2007-08-16T20:12:55Z")

</div>

removed\_email\_address@domain.invalid wrote:

> First question, how can I change network to host byte order? ( like  
> ntoh?)  
> And then how can i put this byte stream into a struct or smtg else.  
> (The real aim is getting x,y,z quickly)

The suggestions to use pack and unpack are right on target, and learning  
how to use them will be very valuable. At some point, you may want to  
have a way of declaratively working with binary data structures, and  
there are a couple of libraries that can help with that:

binaryparse (available as gem)

bindata (ditto)

bitstruct (my own library, not a gem:  
[http://raa.ruby-lang.org/project/bit-struct](http://raa.ruby-lang.org/project/bit-struct))

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/reading-byte-stream-from-network/112220/6 "2007-08-22T13:07:10Z")

</div>

unknown wrote:

> Yeah, look at String#reverse in conjuction w/ unpack. One of the  
> coolest and most confusing aspects of ruby (for me as a C programmer  
> anyway) is the equivalence between strings and unstructured binary.  
> It’s confusing b/c buffers of characters in C in a way have “duck  
> typing” as you usually read them in as an unsigned char \*. Usually  
> with OO languages other than ruby, you have to go through some madness  
> to get unstructured data into a string, or vice versa.

I didnt think it would work in the first run. But hell it worked!

You should have seen the faces of my friend when i showed them this  
single line is actually equal to that ^+%+%&^+%^+ lines of C code:=)  
Respect…

def processTrackDataMessage(message)  
trackDataMsg=message.unpack(‘SIS3D2CDSDSDSDS3A6S36DS10D4S2ISA36A12A16IS2A36A12A36A12’)  
end

Thanks for the tip guys.

Akif,
