Class: Integer

Public Visibility

Public Instance Method Summary

#bytes(address_length, endian = :little)

Extracts a sequence of bytes which represent the Integer.

Returns: Array

#hex_escape

Returns: String

#pack(arch, address_length = arch.address_length)

Packs the Integer into a String, for a specific architecture and.

Returns: String

Public Instance Method Details

bytes

public Array bytes(address_length, endian = :little)

Extracts a sequence of bytes which represent the Integer.

Meta Tags

Examples

  0xff41.bytes(2)
  # => [65, 255]
  0xff41.bytes(4, :big)
  # => [0, 0, 255, 65]

Parameters:

[Integer] address_length

The number of bytes to decode from the Integer.

[Symbol] endian

The endianness to use while decoding the bytes of the Integer. May be either :big, :little or :net.

Returns:

[Array]

The bytes decoded from the Integer.

[View source]


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 46

def bytes(address_length,endian=:little)
  endian = endian.to_s
  buffer = []

  if (endian == 'little' || endian == 'net')
    mask = 0xff

    address_length.times do |i|
      buffer << ((self & mask) >> (i*8))
      mask <<= 8
    end
  elsif endian == 'big'
    mask = (0xff << ((address_length-1)*8))

    address_length.times do |i|
      buffer << ((self & mask) >> ((address_length-i-1)*8))
      mask >>= 8
    end
  end

  return buffer
end

hex_escape

public String hex_escape

Meta Tags

Example:

  42.hex_escape
  # => "\\x2a"

Returns:

[String]

The hex escaped version of the Integer.

[View source]


100
101
102
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 100

def hex_escape
  "\\x%.2x" % self
end

pack

public String pack(arch, address_length = arch.address_length)

Packs the Integer into a String, for a specific architecture and address-length.

Meta Tags

Examples

  0x41.pack(Arch.i686) # => "A\000\000\000"
  0x41.pack(Arch.ppc,2) # => "\000A"

Parameters:

[Ronin::Arch] arch

The architecture to pack the Integer for.

[Integer] address_length

The number of bytes to pack.

Returns:

[String]

The packed Integer.

[View source]


88
89
90
# File 'lib/ronin/formatting/extensions/binary/integer.rb', line 88

def pack(arch,address_length=arch.address_length)
  bytes(address_length,arch.endian).map { |b| b.chr }.join
end
Generated on Friday, September 25 2009 at 02:57:08 PM by YARD 0.2.3.5 (ruby-1.8.6).