[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

integer to byte array?

ilhamik

4/22/2006 9:12:00 AM

Hi,
Could someone tell me how to convert an number (fixnum or float), say
500000, to a byte array? I need to send that byte array to network.

in Java it was so,

byte[] bytes = new byte[4];
int i = 50000;
bytes[0] = (i & 0xff000000) >> 24; // or (byte)((i >> 24) & 0xFF)
bytes[1] = (i & 0xff0000) >> 16;
bytes[2] = (i & 0xff00) >> 8;
bytes[3] = (i & 0xff);

how is it in ruby world?

8 Answers

Robert Klemme

4/22/2006 10:01:00 AM

0

ilhamik <ilhami.kilic@gmail.com> wrote:
> Hi,
> Could someone tell me how to convert an number (fixnum or float), say
> 500000, to a byte array? I need to send that byte array to network.
>
> in Java it was so,
>
> byte[] bytes = new byte[4];
> int i = 50000;
> bytes[0] = (i & 0xff000000) >> 24; // or (byte)((i >> 24) & 0xFF)
> bytes[1] = (i & 0xff0000) >> 16;
> bytes[2] = (i & 0xff00) >> 8;
> bytes[3] = (i & 0xff);
>
> how is it in ruby world?

Use Array#pack and String#unpack

>> [1].pack "I"
=> "\001\000\000\000"

See here for options:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#Str...

Kind regards

robert

minkoo.seo@gmail.com

4/22/2006 10:04:00 AM

0

I don't know what you're doing exactly. To me it does not make sense to
convert integer i into byte array and send them, because there're
already some method to write Integer to network.

Anyway, I believe what you need is Array#pack. Have a look at it.

Sincerely,
Minkoo Seo

ilhamik

4/22/2006 10:12:00 AM

0

I think it is not that i am looking for,
the java version print as result [0,7,-95,32] for 500000

how can i get the same result in ruby?

jh+ruby-lang

4/22/2006 10:25:00 AM

0

ilhamik wrote:
> I think it is not that i am looking for,
> the java version print as result [0,7,-95,32] for 500000
>
> how can i get the same result in ruby?
>
[500000].pack("N").unpack("cccc")

or

[500000].pack("I").unpack("cccc")

depending on required endianess (and see other Array.pack options for
signess).

Maybe someone else will come along with a more elegant way.

-j

Sergey Volkov

4/22/2006 11:18:00 AM

0

Sat, Apr 22 2006 6:12 am "ilhamik" <ilhami.kilic@gmail.com> wrote:
>I think it is not that i am looking for,
> the java version print as result [0,7,-95,32] for 500000
>
> how can i get the same result in ruby?

Use String object in Ruby for byte array in Java,
use String#<< to collect characters/bytes in Ruby String:
% cat tByteArr.rb
# 'Java array' implemented in Ruby (do not reproduce in your code!):
i=500000
bj=' '*4
bj[0] = (i&0xff000000)>>24
bj[1] = (i&0xff0000)>>16
bj[2] = (i&0xff00)>>8
bj[3] = i&0xff

# Ruby String
br=''
4.times{
br << (i&0xFF)
i >>= 8
}
br.reverse!

# check bj and br are the same
fail "different!" unless bj == br

# print out byte codes
br.each_byte{|c|
print c, " "
}
puts
__END__

% ruby tByteArr.rb
0 7 161 32

bytes are signed in Java, unsigned in Ruby,
so you can see 161 instead of -95,
internal bit representation in memory is the same;

Now you know how to reimplement your Java code in Ruby one-to-one,
but in your case. imho, better use pack/unpack Ruby feature.

good luck,
Sergey

Erik Hollensbe

4/22/2006 10:07:00 PM

0

On 2006-04-22 02:11:55 -0700, "ilhamik" <ilhami.kilic@gmail.com> said:

> Hi,
> Could someone tell me how to convert an number (fixnum or float), say
> 500000, to a byte array? I need to send that byte array to network.
>
> in Java it was so,
>
> byte[] bytes = new byte[4];
> int i = 50000;
> bytes[0] = (i & 0xff000000) >> 24; // or (byte)((i >> 24) & 0xFF)
> bytes[1] = (i & 0xff0000) >> 16;
> bytes[2] = (i & 0xff00) >> 8;
> bytes[3] = (i & 0xff);
>
> how is it in ruby world?

There are a few ways. If you just want the string value:

500000.to_s(2) # 2 is the base of the number you want.

If you want a more integer-based version, you can use the pack methods
that have been described:

500000.pack("V")

Or you can just do it the old fashioned way:

def my_32_bit_int_conv(i)
retval = []
4.times { |x| retval.push(0x000000FF & (i >> (x*8)))
return retval
end


ilhamik

4/22/2006 11:40:00 PM

0

thank you,
[500000].pack("N").unpack("cccc") is what i have been looking for.

ilhamik

4/25/2006 7:27:00 PM

0

now I can convert integers successfuly to byte array but i have a
problem with Floats.
I use
a=34.4.pack("e").unpack("c*") to convert the float value into byte
array and then if i convert it back to float via
a.pack("c*").unpack("e").first

it givs me 34.4000015258789
why are thay different?