Class: PacketGen::Types::Int Abstract
- Inherits:
-
Object
- Object
- PacketGen::Types::Int
- Includes:
- Fieldable
- Defined in:
- lib/packetgen/types/int.rb
Overview
This class is abstract.
Base integer class to handle binary integers
Instance Attribute Summary collapse
-
#default ⇒ Integer
Integer default value.
-
#endian ⇒ :little, ...
Integer endianness.
-
#value ⇒ Integer
Integer value.
-
#width ⇒ Integer
Integer size, in bytes.
Instance Method Summary collapse
-
#format_inspect ⇒ String
Format Int type when inspecting header or packet.
-
#initialize(value = nil, endian = nil, width = nil, default = 0) ⇒ Int
constructor
A new instance of Int.
-
#nbits ⇒ Integer
Return the number of bits used to encode this Int.
-
#read(value) ⇒ self
abstract
Read an Int from a binary string or an integer.
-
#sz ⇒ Integer
Give size in bytes of self.
-
#to_f ⇒ Float
Convert Int to Float.
-
#to_i ⇒ Integer
(also: #to_human)
Convert Int to Integer.
- #to_s ⇒ ::String abstract
Methods included from Fieldable
Constructor Details
#initialize(value = nil, endian = nil, width = nil, default = 0) ⇒ Int
Returns a new instance of Int.
36 37 38 39 40 41 |
# File 'lib/packetgen/types/int.rb', line 36 def initialize(value=nil, endian=nil, width=nil, default=0) @value = value @endian = endian @width = width @default = default end |
Instance Attribute Details
#default ⇒ Integer
Integer default value
30 31 32 |
# File 'lib/packetgen/types/int.rb', line 30 def default @default end |
#endian ⇒ :little, ...
Integer endianness
24 25 26 |
# File 'lib/packetgen/types/int.rb', line 24 def endian @endian end |
#value ⇒ Integer
Integer value
20 21 22 |
# File 'lib/packetgen/types/int.rb', line 20 def value @value end |
#width ⇒ Integer
Integer size, in bytes
27 28 29 |
# File 'lib/packetgen/types/int.rb', line 27 def width @width end |
Instance Method Details
#format_inspect ⇒ String
Format Int type when inspecting header or packet
90 91 92 |
# File 'lib/packetgen/types/int.rb', line 90 def format_inspect format_str % [to_i.to_s, to_i] end |
#nbits ⇒ Integer
Return the number of bits used to encode this Int
97 98 99 |
# File 'lib/packetgen/types/int.rb', line 97 def nbits width * 8 end |
#read(value) ⇒ self
This method is abstract.
Read an Int from a binary string or an integer
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/packetgen/types/int.rb', line 48 def read(value) @value = if value.is_a?(Integer) value.to_i elsif defined? @packstr value.to_s.unpack1(@packstr[@endian]) else raise ParseError, 'Int#read is abstract and cannot read' end self end |
#sz ⇒ Integer
Give size in bytes of self
84 85 86 |
# File 'lib/packetgen/types/int.rb', line 84 def sz width end |
#to_f ⇒ Float
Convert Int to Float
78 79 80 |
# File 'lib/packetgen/types/int.rb', line 78 def to_f to_i.to_f end |
#to_i ⇒ Integer Also known as: to_human
Convert Int to Integer
70 71 72 |
# File 'lib/packetgen/types/int.rb', line 70 def to_i @value || @default end |
#to_s ⇒ ::String
This method is abstract.
62 63 64 65 66 |
# File 'lib/packetgen/types/int.rb', line 62 def to_s raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr [to_i].pack(@packstr[@endian]) end |