Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/huawei_solar/array_ext.rb
Overview
Instance Method Summary collapse
-
#to_32i ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit signed int in big-endian order, halving the size.
-
#to_32i_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit signed int in little-endian order, halving the size.
- #to_32u ⇒ Object
-
#to_32u_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit unsigned int in little-endian order, halving the size.
- #to_signed32(number) ⇒ Object
Instance Method Details
#to_32i ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit signed int in big-endian order, halving the size
23 24 25 26 27 |
# File 'lib/huawei_solar/array_ext.rb', line 23 def to_32i raise "Array requires an even number of elements to pack to 32bits: was #{size}" unless size.even? to_32u.map { |n| to_signed32(n) } end |
#to_32i_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit signed int in little-endian order, halving the size
30 31 32 33 34 |
# File 'lib/huawei_solar/array_ext.rb', line 30 def to_32i_le raise "Array requires an even number of elements to pack to 32bits: was #{size}" unless size.even? to_32u_le.map { |n| to_signed32(n) } end |
#to_32u ⇒ Object
5 6 7 8 9 |
# File 'lib/huawei_solar/array_ext.rb', line 5 def to_32u raise "Array requires an even number of elements to pack to 32bits: was #{size}" unless size.even? each_slice(2).map { |(msb, lsb)| [msb, lsb].pack("n*").unpack1("N") } end |
#to_32u_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit unsigned int in little-endian order, halving the size
12 13 14 15 16 |
# File 'lib/huawei_solar/array_ext.rb', line 12 def to_32u_le raise "Array requires an even number of elements to pack to 32bits: was #{size}" unless size.even? each_slice(2).map { |(lsb, msb)| [msb, lsb].pack("n*").unpack1("N") } end |
#to_signed32(number) ⇒ Object
18 19 20 |
# File 'lib/huawei_solar/array_ext.rb', line 18 def to_signed32(number) number >= 2_147_483_648 ? number - 4_294_967_296 : number end |