class Integer

Copyright © 2006-2024 Hal Brodigan (postmodern.mod3 at gmail.com)

ronin-support is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ronin-support is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with ronin-support. If not, see <www.gnu.org/licenses/>.

Public Instance Methods

base36_encode() click to toggle source
Base36

encodes the Integer.

[Base36]: en.wikipedia.org/wiki/Base36

@return [String]

The Base36 encoded String.

@example

1000.base36_encode
# => "rs"

@api public

@since 1.1.0

# File lib/ronin/support/encoding/base36/core_ext/integer.rb, line 39
def base36_encode
  Ronin::Support::Encoding::Base36.encode_int(self)
end
base62_encode() click to toggle source
Base62

encodes the Integer.

[Base62]: en.wikipedia.org/wiki/Base62

@return [String]

The Base62 encoded String.

@example

1337.base62_encode
# => "LZ"

@api public

@since 1.1.0

# File lib/ronin/support/encoding/base62/core_ext/integer.rb, line 39
def base62_encode
  Ronin::Support::Encoding::Base62.encode_int(self)
end
bash_encode()
Alias for: shell_encode
bash_escape()
Alias for: shell_escape
bit_flips(bits) click to toggle source

Returns every bit flip in the integer.

@param [Integer, Range(Integer)] bits

The number of bits to flip or a range of bit indexes to flip.

@return [Array<Integer>]

The bit-flipped integers.

@raise [ArgumentError]

The given bits must be either a Range or an Integer.

@example bit-flip all eight bits:

0x41.bit_flips(8)

@example bit-flip bits 8-16:

0xffff.bit_flips(8...16)

@api public

# File lib/ronin/support/binary/bit_flip/core_ext/integer.rb, line 73
def bit_flips(bits)
  Ronin::Support::Binary::BitFlip::Integer.bit_flips(self,bits)
end
Also aliased as: flip_bits
c_char()
Alias for: c_escape
c_encode() click to toggle source

Formats the Integer as a C escaped String.

@return [String]

The escaped C character.

@example

0x41.c_encode
# => "\\x41"
0x100.c_encode
# => "\\u1000"
0x10000.c_encode
# => "\\U000100000"

@see Ronin::Support::Encoding::C.encode_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/integer.rb, line 78
def c_encode
  Ronin::Support::Encoding::C.encode_byte(self)
end
c_escape() click to toggle source

Escapes the Integer as a C character.

@return [String]

The escaped C character.

@raise [RangeError]

The integer value is negative.

@example

0x41.c_escape
# => "A"
0x22.c_escape
# => "\\\""
0x7f.c_escape
# => "\\x7F"

@example Escaping unicode characters:

0xffff.c_escape
# => "\\uFFFF"
0x10000.c_escape
# => "\\U000100000"

@see Ronin::Support::Encoding::C.escape_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/integer.rb, line 52
def c_escape
  Ronin::Support::Encoding::C.escape_byte(self)
end
Also aliased as: c_char
each_bit_flip(bits,&block) click to toggle source

Enumerates over every bit flip in the integer.

@param [Integer, Range(Integer)] bits

The number of bits to flip or a range of bit indexes to flip.

@yield [int]

If a block is given, it will be passed each bit-flipped integer.

@yieldparam [Integer] int

The integer but with one of it's bits flipped.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.

@raise [ArgumentError]

The given bits must be either a Range or an Integer.

@example bit-flip all eight bits:

0x41.each_bit_flip(8) { |int| puts "%.8b" % int }

@example bit-flip bits 8-16:

0xffff.each_bit_flip(8...16) { |int| puts "%.16b" % int }

@api public

# File lib/ronin/support/binary/bit_flip/core_ext/integer.rb, line 49
def each_bit_flip(bits,&block)
  Ronin::Support::Binary::BitFlip::Integer.each_bit_flip(self,bits,&block)
end
flip_bits(bits)
Alias for: bit_flips
hex_char()
Alias for: hex_escape
hex_encode() click to toggle source

Converts the integer into hex format.

@return [String]

The hex encoded version of the Integer.

@example

0x41.hex_encode
# => "41"

@see Ronin::Support::Encoding::Hex.encode_byte

@since 0.6.0

# File lib/ronin/support/encoding/hex/core_ext/integer.rb, line 37
def hex_encode
  Ronin::Support::Encoding::Hex.encode_byte(self)
end
Also aliased as: to_hex
hex_escape() click to toggle source

Converts the integer into an escaped hex character.

@return [String]

The hex escaped version of the Integer.

@raise [RangeError]

The integer value is negative.

@example

42.hex_char
# => "\\x2a"

@see Ronin::Support::Encoding::Hex.escape_byte

@api public

# File lib/ronin/support/encoding/hex/core_ext/integer.rb, line 60
def hex_escape
  Ronin::Support::Encoding::Hex.escape_byte(self)
end
Also aliased as: hex_char
hex_int() click to toggle source

Encodes the number as a ‘0xXX` hex integer.

@return [String]

The hex encoded integer.

@example

42.hex_int
# => "0x2e"

@api public

# File lib/ronin/support/encoding/hex/core_ext/integer.rb, line 78
def hex_int
  "0x%.2x" % self
end
html_encode(**kwargs) click to toggle source

Encodes the Integer as a HTML String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

Controls whether the escaped characters will be left-padded with
up to seven `0` characters.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The encoded HTML String.

@raise [ArgumentError]

The `format:` or `case:` keyword argument is invalid.

@example

0x41.html_encode
# => "&#65;"

@example Zero-padding:

0x41.html_encode(zero_pad: true)
# => "&#0000065;"

@example Hexadecimal escaped characters:

0x41.html_encode(format: :hex)
# => "&#x41;"

@example Uppercase hexadecimal escaped characters:

0xff.html_encode(format: :hex, case: :upper)
# => "&#XFF;"

@since 0.2.0

@see Ronin::Support::Encoding::HTML.encode_byte

@api public

# File lib/ronin/support/encoding/html/core_ext/integer.rb, line 102
def html_encode(**kwargs)
  Ronin::Support::Encoding::HTML.encode_byte(self,**kwargs)
end
html_escape(**kwargs) click to toggle source

Escapes the Integer as an HTML String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The escaped HTML String.

@raise [ArgumentError]

The `case:` keyword argument is invalid.

@example

0x26.html_escape
# => "&amp;"

@example Uppercase encoding:

0x26.html_escape(case: :upper)
# => "&AMP;"

@since 0.2.0

@see Ronin::Support::Encoding::HTML.escape_byte

@api public

# File lib/ronin/support/encoding/html/core_ext/integer.rb, line 53
def html_escape(**kwargs)
  Ronin::Support::Encoding::HTML.escape_byte(self,**kwargs)
end
http_encode(**kwargs) click to toggle source

Encodes the byte as an escaped HTTP decimal character.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The encoded HTTP byte.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

0x41.http_encode
# => "%41"

@example Lowercase encoding:

0xff.http_encode(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::HTTP.encode_byte

@api public

# File lib/ronin/support/encoding/http/core_ext/integer.rb, line 54
def http_encode(**kwargs)
  Ronin::Support::Encoding::HTTP.encode_byte(self,**kwargs)
end
http_escape(**kwargs) click to toggle source

HTTP escapes the Integer.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The HTTP escaped form of the Integer.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@raise [RangeError]

The byte value is negative or greater than 255.

@example

62.http_escape
# => "%3E"

@example Lowercase encoding:

0xff.http_escape(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::HTTP.escape_byte

@api public

@since 0.6.0

# File lib/ronin/support/encoding/http/core_ext/integer.rb, line 91
def http_escape(**kwargs)
  Ronin::Support::Encoding::HTTP.escape_byte(self,**kwargs)
end
js_encode() click to toggle source

Encodes the Integer as a JavaScript character.

@return [String]

The encoded JavaScript character.

@example

0x41.js_encode
# => "\\x41"

@see Ronin::Support::Encoding::JS.encode_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/js/core_ext/integer.rb, line 63
def js_encode
  Ronin::Support::Encoding::JS.encode_byte(self)
end
js_escape() click to toggle source

Escapes the Integer as a JavaScript character.

@return [String]

The escaped JavaScript character.

@example

0x41.js_escape
# => "A"
0x22.js_escape
# => "\\\""
0x7f.js_escape
# => "\\x7F"

@see Ronin::Support::Encoding::JS.escape_byte

@since 0.2.0

@api public

# File lib/ronin/support/encoding/js/core_ext/integer.rb, line 43
def js_escape
  Ronin::Support::Encoding::JS.escape_byte(self)
end
pack(argument, **kwargs) click to toggle source

Packs the Integer into a String.

@param [String, Symbol] argument

The `Array#pack` String or {Ronin::Support::Binary::Template} type.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for
{Ronin::Support::Binary::CTypes.platform}.

@option kwargs [:little, :big, :net, nil] :endian

The desired endianness of the binary format.

@option kwargs [:x86, :x86_64,

              :ppc, :ppc64,
              :mips, :mips_le, :mips_be,
              :mips64, :mips64_le, :mips64_be,
              :arm, :arm_le, :arm_be,
              :arm64, :arm64_le, :arm64_be] :arch
The desired architecture of the binary format.

@option kwargs [:linux, :macos, :windows,

              :android, :apple_ios, :bsd,
              :freebsd, :openbsd, :netbsd] :os
The Operating System name to lookup.

@return [String]

The packed Integer.

@raise [ArgumentError]

The given argument was not a `String`, `Symbol`, or valid type name.

@example using a ‘Array#pack` format string:

0x41.pack('V')
# => "A\0\0\0"

@example using {Ronin::Support::Binary::CTypes} types:

0x41.pack(:uint32_le)
# => "A\x00\x00\x00"

@example specifying the endian-ness:

0x41.pack(:uint32, endian: :big)
# => "\x00\x00\x00A"

@example specifying the architecture:

0x41.pack(:ulong, arch: :arm64)
# => "A\x00\x00\x00\x00\x00\x00\x00"

@example specifying the architecture and Operating System (OS):

0x41.pack(:size_t, arch: :arm64, os: :linux)
# => "A\x00\x00\x00\x00\x00\x00\x00"

@see rubydoc.info/stdlib/core/Array:pack @see Ronin::Support::Binary::Template

@api public

# File lib/ronin/support/binary/core_ext/integer.rb, line 80
def pack(argument, **kwargs)
  case argument
  when String
    [self].pack(argument)
  when Symbol
    types = Ronin::Support::Binary::CTypes.platform(**kwargs)
    type  = types[argument]
    type.pack(self)
  else
    raise(ArgumentError,"invalid pack argument: #{argument}")
  end
end
powershell_encode() click to toggle source

Encodes the Integer as a PowerShell character.

@return [String]

The encoded PowerShell character.

@raise [RangeError]

The integer value is negative.

@example

0x41.powershell_encode
# => "[char]0x41"
0x0a.powershell_encode
# => "`n"

@example Encoding unicode characters:

1001.powershell_escape
# => "`u{1001}"

@see Ronin::Support::Encoding::PowerShell.encode_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/powershell/core_ext/integer.rb, line 48
def powershell_encode
  Ronin::Support::Encoding::PowerShell.encode_byte(self)
end
Also aliased as: psh_encode
powershell_escape() click to toggle source

Escapes the Integer as a PowerShell character.

@return [String]

The escaped PowerShell character.

@raise [RangeError]

The integer value is negative.

@example

0x41.powershell_escape
# => "A"
0x08.powershell_escape
# => "`b"
0xff.powershell_escape
# => "[char]0xff"

@example Escaping unicode characters:

1001.powershell_escape
# => "`u{1001}"

@see Ronin::Support::Encoding::PowerShell.escape_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/powershell/core_ext/integer.rb, line 81
def powershell_escape
  Ronin::Support::Encoding::PowerShell.escape_byte(self)
end
Also aliased as: psh_escape
psh_encode()
Alias for: powershell_encode
psh_escape()
Alias for: powershell_escape
sh_encode()
Alias for: shell_encode
sh_escape()
Alias for: shell_escape
shell_encode() click to toggle source

Encodes the Integer as a shell character.

@return [String]

The encoded shell character.

@raise [RangeError]

The integer value is negative.

@example

0x41.shell_encode
# => "\\x41"
0x0a.shell_encode
# => "\\n"

@example Encoding unicode characters:

1001.shell_encode
# => "\\u1001"

@see Ronin::Support::Encoding::Shell.encode_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/shell/core_ext/integer.rb, line 48
def shell_encode
  Ronin::Support::Encoding::Shell.encode_byte(self)
end
Also aliased as: sh_encode, bash_encode
shell_escape() click to toggle source

Escapes the Integer as a shell character.

@return [String]

The escaped shell character.

@raise [RangeError]

The integer value is negative.

@example

0x41.shell_escape
# => "A"
0x08.shell_escape
# => "\b"
0xff.shell_escape
# => "\xff"

@example Escaping unicode characters:

1001.shell_escape
# => "\\u1001"

@see Ronin::Support::Encoding::Shell.escape_byte

@since 1.0.0

@api public

# File lib/ronin/support/encoding/shell/core_ext/integer.rb, line 82
def shell_escape
  Ronin::Support::Encoding::Shell.escape_byte(self)
end
Also aliased as: sh_escape, bash_escape
to_hex() click to toggle source

Converts the Integer to hexadecimal.

@return [String]

The integer in hexadecimal format.

@api public

@since 1.0.0

# File lib/ronin/support/core_ext/integer.rb, line 33
def to_hex
  to_s(16)
end
to_i16()
Alias for: to_int16
to_i32()
Alias for: to_int32
to_i64()
Alias for: to_int64
to_i8()
Alias for: to_int8
to_int16() click to toggle source

Converts the integer into an 16-bit signed integer.

@return [Integer]

The integer truncated to 16-bits with signed preserved.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 190
def to_int16
  int = self & 0xffff

  if int[15] == 1
    # interpret the new signed bit
    int -= 0x10000
  end

  return int
end
Also aliased as: to_i16
to_int32() click to toggle source

Converts the integer into an 32-bit signed integer.

@return [Integer]

The integer truncated to 32-bits with signed preserved.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 213
def to_int32
  int = self & 0xffffffff

  if int[31] == 1
    # interpret the new signed bit
    int -= 0x100000000
  end

  return int
end
Also aliased as: to_i32
to_int64() click to toggle source

Converts the integer into an 64-bit signed integer.

@return [Integer]

The integer truncated to 64-bits with signed preserved.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 236
def to_int64
  int = self & 0xffffffffffffffff

  if int[63] == 1
    # interpret the new signed bit
    int -= 0x10000000000000000
  end

  return int
end
Also aliased as: to_i64
to_int8() click to toggle source

Converts the integer into an 8-bit signed integer.

@return [Integer]

The integer truncated to 8-bits with signed preserved.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 167
def to_int8
  int = self & 0xff

  if int[7] == 1
    # interpret the new signed bit
    int -= 0x100
  end

  return int
end
Also aliased as: to_i8
to_u16()
Alias for: to_uint16
to_u32()
Alias for: to_uint32
to_u64()
Alias for: to_uint64
to_u8()
Alias for: to_uint8
to_uint16() click to toggle source

Converts the integer into an 16-bit unsigned integer.

@return [Integer]

The integer truncated to 16-bits.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 119
def to_uint16
  self & 0xffff
end
Also aliased as: to_u16
to_uint32() click to toggle source

Converts the integer into an 32-bit unsigned integer.

@return [Integer]

The integer truncated to 32-bits.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 135
def to_uint32
  self & 0xffffffff
end
Also aliased as: to_u32
to_uint64() click to toggle source

Converts the integer into an 64-bit unsigned integer.

@return [Integer]

The integer truncated to 64-bits.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 151
def to_uint64
  self & 0xffffffffffffffff
end
Also aliased as: to_u64
to_uint8() click to toggle source

Converts the integer into an 8-bit unsigned integer.

@return [Integer]

The integer truncated to 8-bits.

@api public

@since 1.0.0

# File lib/ronin/support/binary/core_ext/integer.rb, line 103
def to_uint8
  self & 0xff
end
Also aliased as: to_u8
uri_encode(**kwargs) click to toggle source

URI encodes the byte.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI encoded byte.

@example

0x41.uri_encode
# => "%41"

@example Lowercase encoding:

0xff.uri_encode(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::URI.encode_byte

@api public

# File lib/ronin/support/encoding/uri/core_ext/integer.rb, line 48
def uri_encode(**kwargs)
  Ronin::Support::Encoding::URI.encode_byte(self,**kwargs)
end
uri_escape(**kwargs) click to toggle source

URI escapes the byte.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI escaped byte.

@example

0x41.uri_escape
# => "A"
0x3d.uri_escape
# => "%3D"

@example Lowercase encoding:

0xff.uri_escape(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::URI.escape_byte

@api public

# File lib/ronin/support/encoding/uri/core_ext/integer.rb, line 79
def uri_escape(**kwargs)
  Ronin::Support::Encoding::URI.escape_byte(self,**kwargs)
end
uri_form_encode(**kwargs) click to toggle source

URI Form encodes the Integer.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI Form encoded Integer.

@example

0x41.uri_form_encode
# => "%41"
0x20.uri_form_encode
# => "+"

@example Lowercase encoding:

0xff.uri_form_encode(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::URI::Form.encode_byte

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uri/core_ext/integer.rb, line 145
def uri_form_encode(**kwargs)
  Ronin::Support::Encoding::URI::Form.encode_byte(self,**kwargs)
end
uri_form_escape(**kwargs) click to toggle source

URI Form escapes the Integer.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI Form escaped Integer.

@example

0x41.uri_form_ecape
# => "A"
0x20.uri_form_escape
# => "+"

@example Lowercase encoding:

0xff.uri_form_escape(case: :lower)
# => "%ff"

@see Ronin::Support::Encoding::URI::Form.escape_byte

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uri/core_ext/integer.rb, line 112
def uri_form_escape(**kwargs)
  Ronin::Support::Encoding::URI::Form.escape_byte(self,**kwargs)
end
xml_encode(**kwargs) click to toggle source

Encodes the Integer as a XML String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

Controls whether the escaped characters will be left-padded with
up to seven `0` characters.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The XML String.

@example

0x41.xml_encode
# => "&#65;"

@example Zero-padding:

0x41.xml_encode(zero_pad: true)
# => "&#0000065;"

@example Hexadecimal escaped characters:

0x41.xml_encode(format: :hex)
# => "&#x41;"

@example Uppercase hexadecimal escaped characters:

0xff.xml_encode(format: :hex, case: :upper)
# => "&#XFF;"

@see Ronin::Support::Encoding::XML.encode_byte

@since 0.2.0

@api public

# File lib/ronin/support/encoding/xml/core_ext/integer.rb, line 96
def xml_encode(**kwargs)
  Ronin::Support::Encoding::XML.encode_byte(self,**kwargs)
end
xml_escape(**kwargs) click to toggle source

Escapes the Integer as an XML String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The escaped XML String.

@example

0x26.xml_escape
# => "&amp;"

@example Uppercase encoding:

0x26.xml_escape(case: :upper)
# => "&AMP;"

@see Ronin::Support::Encoding::XML.escape_byte

@since 0.2.0

@api public

# File lib/ronin/support/encoding/xml/core_ext/integer.rb, line 50
def xml_escape(**kwargs)
  Ronin::Support::Encoding::XML.escape_byte(self,**kwargs)
end