Class: PacketGen::Types::Int24

Inherits:
Int
  • Object
show all
Defined in:
lib/packetgen/types/int.rb

Overview

3-byte unsigned integer

Author:

  • Sylvain Daubert

Since:

  • 2.1.4

Direct Known Subclasses

Int24be, Int24le, Int24n

Instance Attribute Summary

Attributes inherited from Int

#default, #endian, #value, #width

Instance Method Summary collapse

Methods inherited from Int

#format_inspect, #nbits, #sz, #to_f, #to_i

Methods included from Fieldable

#format_inspect, #sz, #to_human, #type_name

Constructor Details

#initialize(value = nil, endian = :big) ⇒ Int24

Returns a new instance of Int24.

Parameters:

  • value (Integer, nil) (defaults to: nil)
  • endian (:big, :little, :native) (defaults to: :big)

Since:

  • 2.1.4



223
224
225
226
227
228
229
230
231
232
# File 'lib/packetgen/types/int.rb', line 223

def initialize(value=nil, endian=:big)
  if endian == :native
    endian = if [1].pack('S').unpack1('n') == 1
               :big
             else
               :little
             end
  end
  super(value, endian, 3)
end

Instance Method Details

#read(value) ⇒ self

Read an 3-byte Int from a binary string or an integer

Parameters:

Returns:

  • (self)

Since:

  • 2.1.4



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/packetgen/types/int.rb', line 237

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

Returns:

  • (::String)

Since:

  • 2.1.4



255
256
257
258
259
260
261
262
263
# File 'lib/packetgen/types/int.rb', line 255

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