integer to byte array?

I

ilhamik

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?
 
R

Robert Klemme

ilhamik 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?

Use Array#pack and String#unpack
=> "\001\000\000\000"

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

Kind regards

robert
 
M

Minkoo Seo

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
 
I

ilhamik

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?
 
J

Jonathan Hudson

ilhamik said:
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
 
V

vsv

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
 
E

Erik Hollensbe

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
 
I

ilhamik

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top