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.
37 38 39 40 41 42 |
# File 'lib/packetgen/types/int.rb', line 37 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
31 32 33 |
# File 'lib/packetgen/types/int.rb', line 31 def default @default end |
#endian ⇒ :little, ...
Integer endianness
25 26 27 |
# File 'lib/packetgen/types/int.rb', line 25 def endian @endian end |
#value ⇒ Integer
Integer value
21 22 23 |
# File 'lib/packetgen/types/int.rb', line 21 def value @value end |
#width ⇒ Integer
Integer size, in bytes
28 29 30 |
# File 'lib/packetgen/types/int.rb', line 28 def width @width end |
Instance Method Details
#format_inspect ⇒ String
Format Int type when inspecting header or packet
91 92 93 |
# File 'lib/packetgen/types/int.rb', line 91 def format_inspect format_str % [to_i.to_s, to_i] end |
#nbits ⇒ Integer
Return the number of bits used to encode this Int
98 99 100 |
# File 'lib/packetgen/types/int.rb', line 98 def nbits width * 8 end |
#read(value) ⇒ self
This method is abstract.
Read an Int from a binary string or an integer
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/packetgen/types/int.rb', line 49 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
85 86 87 |
# File 'lib/packetgen/types/int.rb', line 85 def sz width end |
#to_f ⇒ Float
Convert Int to Float
79 80 81 |
# File 'lib/packetgen/types/int.rb', line 79 def to_f to_i.to_f end |
#to_i ⇒ Integer Also known as: to_human
Convert Int to Integer
71 72 73 |
# File 'lib/packetgen/types/int.rb', line 71 def to_i @value || @default end |
#to_s ⇒ ::String
This method is abstract.
63 64 65 66 67 |
# File 'lib/packetgen/types/int.rb', line 63 def to_s raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr [to_i].pack(@packstr[@endian]) end |