class Array

Public Instance Methods

from_32f() click to toggle source

Given an array of 32bit Floats, we turn it into an array of 16bit Fixnums, doubling the size

# File lib/rmodbus/ext.rb, line 49
def from_32f
  self.pack('g*').unpack('n*').each_slice(2).map { |arr| arr.reverse }.flatten
end
from_32i() click to toggle source

Given an array of 32bit Fixnum, we turn it into an array of 16bit fixnums, doubling the size

# File lib/rmodbus/ext.rb, line 60
def from_32i
  self.pack('N*').unpack('n*').each_slice(2).map { |arr| arr.reverse }.flatten
end
pack_to_word() click to toggle source
# File lib/rmodbus/ext.rb, line 64
def pack_to_word
  word = 0
  s = ""
  mask = 0x01

  self.each do |bit|
    word |= mask if bit > 0
    mask <<= 1
    if mask  == 0x100
      mask = 0x01
      s << word.chr
      word = 0
    end
  end
  unless mask == 0x01
    s << word.chr
  else
    s
  end
end
to_32f() click to toggle source

Given an array of 16bit Fixnum, we turn it into 32bit Int in big-endian order, halving the size

# File lib/rmodbus/ext.rb, line 37
def to_32f
  raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even?
  self.each_slice(2).map { |(lsb, msb)| [msb, lsb].pack('n*').unpack('g')[0] }
end
to_32f_le() click to toggle source

Given an array of 16bit Fixnum, we turn it into 32bit Int in little-endian order, halving the size

# File lib/rmodbus/ext.rb, line 43
def to_32f_le
  raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even?
  self.each_slice(2).map { |(lsb, msb)| [lsb, msb].pack('n*').unpack('g')[0] }
end
to_32i() click to toggle source

Given an array of 16bit Fixnum, we turn it into 32bit Float in big-endian order, halving the size

# File lib/rmodbus/ext.rb, line 54
def to_32i
  raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even?
  self.each_slice(2).map { |(lsb, msb)| [msb, lsb].pack('n*').unpack('N')[0] }
end