[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby and byte

Agnieszka Figiel

7/12/2006 9:29:00 AM

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-....

3 Answers

Pedro Côrte-Real

7/12/2006 9:50:00 AM

0

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.

Carlos

7/12/2006 10:49:00 AM

0

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.
--



Joel VanderWerf

7/12/2006 8:21:00 PM

0

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/b...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407