Class: Array

Inherits:
Object show all
Defined in:
lib/ronin/formatting/extensions/text/array.rb,
lib/ronin/formatting/extensions/binary/array.rb

Overview

Copyright (c) 2006-2013 Hal Brodigan (postmodern.mod3 at gmail.com)

This file is part of Ronin Support.

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 http://www.gnu.org/licenses/.

Instance Method Summary collapse

Instance Method Details

#bytesArray

Decodes the bytes contained within the Array. The Array may contain both Integer and String objects.

Examples:

[0x41, 0x41, 0x20].bytes
# => [0x41, 0x41, 0x20]
['A', 'BB', 0x90].bytes
# => [0x41, 0x42, 0x42, 0x90]

Returns:

  • (Array)

    The bytes contained within the Array.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ronin/formatting/extensions/text/array.rb', line 39

def bytes
  bytes = []

  each do |element|
    case element
    when Integer then bytes << element
    else
      element.to_s.each_byte do |b|
        bytes << b
      end
    end
  end

  return bytes
end

#char_stringString

Returns The String created from the characters within the Array.

Examples:

[0x41, 0x41, 0x20].char_string
# => "AA "

Returns:

  • (String)

    The String created from the characters within the Array.



85
86
87
# File 'lib/ronin/formatting/extensions/text/array.rb', line 85

def char_string
  chars.join
end

#charsArray

Decodes the characters contained within the Array. The Array may contain either Integer or String objects.

Examples:

[0x41, 0x41, 0x20].chars
# => ["A", "A", " "]

Returns:

  • (Array)

    The characters generated from the array.



68
69
70
71
72
73
# File 'lib/ronin/formatting/extensions/text/array.rb', line 68

def chars
  array_bytes = bytes
  array_bytes.map! { |b| b.chr }

  return array_bytes
end

#hex_charsArray<String>

Decodes the bytes contained with the Array, and escapes them as hexadecimal characters.

Examples:

[0x41, 0x41, 0x20].bytes
# => ['\x41', '\x41', '\x20']
['A', 'BB', 0x90].bytes
# => ['\x41', '\x42', '\x42', '\x90']

Returns:

  • (Array<String>)

    The hexadecimal characters contained within the Array.



106
107
108
109
110
111
# File 'lib/ronin/formatting/extensions/text/array.rb', line 106

def hex_chars
  array_bytes = bytes
  array_bytes.map! { |b| '\x%x' % b }

  return array_bytes
end

#hex_integersArray<String>

Decodes the bytes contained with the Array, and escapes them as hexadecimal integers.

Examples:

[0x41, 0x41, 0x20].bytes
# => ['0x41', '0x41', '0x20']
['A', 'BB', 0x90].bytes
# => ['0x41', '0x42', '0x42', '0x90']

Returns:

  • (Array<String>)

    The hexadecimal integers contained within the Array.



130
131
132
133
134
135
# File 'lib/ronin/formatting/extensions/text/array.rb', line 130

def hex_integers
  array_bytes = bytes
  array_bytes.map! { |b| '0x%x' % b }

  return array_bytes
end

#pack(*arguments) ⇒ String

Packs the Array into a String.

Examples:

[0x1234, "hello"].pack(:uint16_le, :string)
# => "\x34\x12hello\0"

using a String#unpack template:

[0x1234, "hello"].pack('vZ*')
# => "\x34\x12hello\0"

Parameters:

Returns:

  • (String)

    The packed Array.

Raises:

See Also:

Since:

  • 0.5.0



53
54
55
56
57
58
59
# File 'lib/ronin/formatting/extensions/binary/array.rb', line 53

def pack(*arguments)
  if (arguments.length == 1 && arguments.first.kind_of?(String))
    pack_original(arguments.first)
  else
    pack_original(Ronin::Binary::Template.compile(arguments))
  end
end

#pack_originalObject



24
# File 'lib/ronin/formatting/extensions/binary/array.rb', line 24

alias pack_original pack