Class: RubyVolt::DataType::Basic
- Inherits:
-
RubyVolt::DataType
- Object
- RubyVolt::DataType
- RubyVolt::DataType::Basic
- Defined in:
- lib/ruby_volt/data_type/basic.rb
Direct Known Subclasses
Class Method Summary collapse
-
.bytesToInt(length, bytes) ⇒ Object
bytes = array of 8-bit unsigned.
- .intToBytes(length, value) ⇒ Object
Methods inherited from RubyVolt::DataType
classifyDataType, testpacking, voltDataType
Class Method Details
.bytesToInt(length, bytes) ⇒ Object
bytes = array of 8-bit unsigned
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_volt/data_type/basic.rb', line 14 def bytesToInt(length, bytes) # bytes = array of 8-bit unsigned bytes = bytes.unpack("C#{length}") most_significant_bit = 1 << 7 negative = (bytes[0] & most_significant_bit) != 0 unscaled_value = -(bytes[0] & most_significant_bit) << length*8-8 # Clear the highest bit # Unleash the powers of the butterfly bytes[0] &= ~most_significant_bit # Get the 2's complement (0..length-1).each {|i| unscaled_value += bytes[i] << ((length-1 - i) * 8)} unscaled_value * -1 if negative unscaled_value end |
.intToBytes(length, value) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/ruby_volt/data_type/basic.rb', line 6 def intToBytes(length, value) bitlength = length*8 div = nil x = [[32, 'N'], [16, 'n'], [8, 'C']].find {|a| (div = bitlength.divmod(a[0])) && div[0] != 0 && div[1] == 0} b = (2**x[0])-1 ([value & b] + (2..div[0]).map {|i| (value >> x[0]*(i-1)) & b}).reverse.pack(x[1]*div[0]) end |