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
[email protected] 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
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
super!
thanks robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs