Class: PacketGen::Types::Int24
Overview
3-byte unsigned integer
Instance Attribute Summary
Attributes inherited from Int
#default, #endian, #value, #width
Instance Method Summary collapse
-
#initialize(value = nil, endian = :big) ⇒ Int24
constructor
A new instance of Int24.
-
#read(value) ⇒ self
Read an 3-byte Int from a binary string or an integer.
- #to_s ⇒ ::String
Methods inherited from Int
Constructor Details
#initialize(value = nil, endian = :big) ⇒ Int24
Returns a new instance of Int24.
171 172 173 |
# File 'lib/packetgen/types/int.rb', line 171 def initialize(value=nil, endian=:big) super(value, endian, 3) end |
Instance Method Details
#read(value) ⇒ self
Read an 3-byte Int from a binary string or an integer
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/packetgen/types/int.rb', line 178 def read(value) return self if value.nil? @value = if value.is_a?(Integer) value.to_i else up8 = down16 = 0 if @endian == :big up8, down16 = value.to_s.unpack('Cn') else down16, up8 = value.to_s.unpack('vC') end (up8 << 16) | down16 end self end |
#to_s ⇒ ::String
196 197 198 199 200 201 202 203 204 |
# File 'lib/packetgen/types/int.rb', line 196 def to_s up8 = to_i >> 16 down16 = to_i & 0xffff if @endian == :big [up8, down16].pack('Cn') else [down16, up8].pack('vC') end end |