Class: Bytepack::Basic
Class Method Summary collapse
-
.bytesToInt(length, bytes, offset = 0) ⇒ Object
bytes = array of 8-bit unsigned.
- .intToBytes(length, value) ⇒ Object
- .preprocess(bytes, offset, format, length = 0) ⇒ Object
Methods inherited from Struct
classifyDataType, config, packingDataType, single_type_array?, testpacking
Class Method Details
.bytesToInt(length, bytes, offset = 0) ⇒ Object
bytes = array of 8-bit unsigned
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bytepack/basic.rb', line 20 def bytesToInt(length, bytes, offset = 0) # bytes = array of 8-bit unsigned format = "C#{length}" format.prepend("@#{offset}") if offset > 0 bytes = bytes.unpack(format) 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
12 13 14 15 16 17 18 |
# File 'lib/bytepack/basic.rb', line 12 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 |
.preprocess(bytes, offset, format, length = 0) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/bytepack/basic.rb', line 5 def preprocess(bytes, offset, format, length = 0) format += length.to_s if !(self <= FixedSize) && length > 1 format = offset > 0 ? "@#{offset}#{format}" : format offset += length [offset, format] end |