class PacketGen::Types::Int
Base integer class to handle binary integers @abstract @author Sylvain Daubert
Attributes
Integer default value @return [Integer]
Integer endianness @return [:little,:big]
Integer value @return [Integer]
Integer value @return [Integer]
Integer size, in bytes @return [Integer]
Public Class Methods
@param [Integer,nil] value @param [:little,:big,nil] endian @param [Integer,nil] width @param [Integer] default
# File lib/packetgen/types/int.rb, line 34 def initialize(value=nil, endian=nil, width=nil, default=0) @value = value @endian = endian @width = width @default = default end
Public Instance Methods
Format Int
type when inspecting header or packet @return [String]
# File lib/packetgen/types/int.rb, line 88 def format_inspect format_str % [to_i.to_s, to_i] end
@abstract Read an Int
from a binary string or an integer @param [Integer, to_s
] value @return [self] @raise [ParseError] when reading #to_s
objects with abstract Int
class.
# File lib/packetgen/types/int.rb, line 46 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
Give size in bytes of self @return [Integer]
# File lib/packetgen/types/int.rb, line 82 def sz width end
Convert Int
to Float @return [Float]
# File lib/packetgen/types/int.rb, line 76 def to_f to_i.to_f end
Convert Int
to Integer @return [Integer]
# File lib/packetgen/types/int.rb, line 68 def to_i @value || @default end
@abstract @return [::String] @raise [ParseError] This is an abstrat method and must be redefined
# File lib/packetgen/types/int.rb, line 60 def to_s raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' unless defined? @packstr [to_i].pack(@packstr[@endian]) end
Private Instance Methods
# File lib/packetgen/types/int.rb, line 94 def format_str "%-16s (0x%0#{width * 2}x)" end