Hi all,
I'd like to convert an int to a byte array in Ruby.
In C# you can do this:
byte[] b = BitConverter.GetBytes(BufferLength);
Any tips for the ruby equivalent gratefully received
Thanks
Steven
Hi all,
I'd like to convert an int to a byte array in Ruby.
In C# you can do this:
byte[] b = BitConverter.GetBytes(BufferLength);
Any tips for the ruby equivalent gratefully received
Thanks
Steven
You can use pack
irb(main):001:0> [5].pack "i*"
=> "\005\000\000\000"
irb(main):002:0> [5].pack "I*"
=> "\005\000\000\000"
irb(main):006:0> [5].pack("I*").split //
=> ["\005", "\000", "\000", "\000"]
But note also that you can treat an int as a bit vector:
irb(main):003:0> 5[2]
=> 1
irb(main):004:0> 5[3]
=> 0
HTH
robert
steven.shingler@virgin.net wrote:
Hi all,
I'd like to convert an int to a byte array in Ruby.
In C# you can do this:
byte b = BitConverter.GetBytes(BufferLength);Any tips for the ruby equivalent gratefully received
super!
thanks robert