Tom M
8/16/2007 3:04:00 PM
On Aug 16, 8:42 am, akifuse...@gmail.com wrote:
> 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,
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.