From: Dennis Newbold Date: 2002-05-20T14:06:59+09:00 Subject: Re: Binary network protocols Hi Terje, Suggest you use array#pack with arg of 'n' for int16 and 'N' for int32. Alternatively, a small Ruby extension written in C. Cf. Pickaxe book pp. 289-290. Good luck. Dennis On Mon, 20 May 2002, Terje Elde wrote: > Hi, > > I'm starting on a projects which requires me to use a spesific network > protocol. This protocol demands that I write certain size fields in Int16, > and other values as Int32. The rest is just plain text. > > While looking around I've found little or no indications of how to write a > number of a spesific size (ie, int16) to a network socket, or how to deal with > it at all. I found a seemingly old int package which would allow me to do > things like 100.prec(Int16), but it doesn't seem to work: > > irb(main):004:0> require '/usr/local/lib/ruby/1.6/site_ruby/i386-freebsd4/num/int.so' > true > irb(main):005:0> 100.prec(Int16) > 100 > irb(main):006:0> foo = 100.prec(Int16) > 100 > irb(main):007:0> putc foo > SystemStackError: stack level too deep > from (irb):7:in `putc' > from (irb):7 > irb(main):008:0> > > The simple goal is to be able to write numbers of size Int16 or Int32 to a > socket. > > Any help appreciated, > Terje >