Class: Unisec::Hexdump

Inherits:
Object
  • Object
show all
Defined in:
lib/unisec/hexdump.rb

Overview

Hexdump of all Unicode encodings.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Hexdump

Init the hexdump.

Examples:

hxd = Unisec::Hexdump.new('I 💕 Ruby 💎')
hxd.utf8 # => "49 20 f0 9f 92 95 20 52 75 62 79 20 f0 9f 92 8e"
hxd.utf16be # => "0049 0020 d83d dc95 0020 0052 0075 0062 0079 0020 d83d dc8e"
hxd.utf32be # => "00000049 00000020 0001f495 00000020 00000052 00000075 00000062 00000079 00000020 0001f48e"

Parameters:

  • str (String)

    Input string to encode



35
36
37
38
39
40
41
# File 'lib/unisec/hexdump.rb', line 35

def initialize(str)
  @utf8 = Hexdump.utf8(str)
  @utf16be = Hexdump.utf16be(str)
  @utf16le = Hexdump.utf16le(str)
  @utf32be = Hexdump.utf32be(str)
  @utf32le = Hexdump.utf32le(str)
end

Instance Attribute Details

#utf16beString (readonly)

UTF-16BE hexdump

Returns:

  • (String)

    UTF-16BE hexdump



14
15
16
# File 'lib/unisec/hexdump.rb', line 14

def utf16be
  @utf16be
end

#utf16leString (readonly)

UTF-16LE hexdump

Returns:

  • (String)

    UTF-16LE hexdump



18
19
20
# File 'lib/unisec/hexdump.rb', line 18

def utf16le
  @utf16le
end

#utf32beString (readonly)

UTF-32BE hexdump

Returns:

  • (String)

    UTF-32BE hexdump



22
23
24
# File 'lib/unisec/hexdump.rb', line 22

def utf32be
  @utf32be
end

#utf32leString (readonly)

UTF-32LE hexdump

Returns:

  • (String)

    UTF-32LE hexdump



26
27
28
# File 'lib/unisec/hexdump.rb', line 26

def utf32le
  @utf32le
end

#utf8String (readonly)

UTF-8 hexdump

Returns:

  • (String)

    UTF-8 hexdump



10
11
12
# File 'lib/unisec/hexdump.rb', line 10

def utf8
  @utf8
end

Class Method Details

.utf16be(str) ⇒ String

Encode to UTF-16BE in hexdump format (spaced at every code unit = every 2 bytes)

Examples:

Unisec::Hexdump.utf16be('🐋') # => "d83d dc0b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-16BE encoded)



57
58
59
# File 'lib/unisec/hexdump.rb', line 57

def self.utf16be(str)
  str.encode('UTF-16BE').to_hex.scan(/.{4}/).join(' ')
end

.utf16le(str) ⇒ String

Encode to UTF-16LE in hexdump format (spaced at every code unit = every 2 bytes)

Examples:

Unisec::Hexdump.utf16le('🐋') # => "3dd8 0bdc"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-16LE encoded)



66
67
68
# File 'lib/unisec/hexdump.rb', line 66

def self.utf16le(str)
  str.encode('UTF-16LE').to_hex.scan(/.{4}/).join(' ')
end

.utf32be(str) ⇒ String

Encode to UTF-32BE in hexdump format (spaced at every code unit = every 4 bytes)

Examples:

Unisec::Hexdump.utf32be('🐋') # => "0001f40b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-32BE encoded)



75
76
77
# File 'lib/unisec/hexdump.rb', line 75

def self.utf32be(str)
  str.encode('UTF-32BE').to_hex.scan(/.{8}/).join(' ')
end

.utf32le(str) ⇒ String

Encode to UTF-32LE in hexdump format (spaced at every code unit = every 4 bytes)

Examples:

Unisec::Hexdump.utf32le('🐋') # => "0bf40100"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-32LE encoded)



84
85
86
# File 'lib/unisec/hexdump.rb', line 84

def self.utf32le(str)
  str.encode('UTF-32LE').to_hex.scan(/.{8}/).join(' ')
end

.utf8(str) ⇒ String

Encode to UTF-8 in hexdump format (spaced at every code unit = every byte)

Examples:

Unisec::Hexdump.utf8('🐋') # => "f0 9f 90 8b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-8 encoded)



48
49
50
# File 'lib/unisec/hexdump.rb', line 48

def self.utf8(str)
  str.encode('UTF-8').to_hex.scan(/.{2}/).join(' ')
end

Instance Method Details

#displayString

Display a CLI-friendly output summurizing the hexdump in all Unicode encodings

Examples:

puts Unisec::Hexdump.new('').display # =>
# UTF-8: e2 84 aa
# UTF-16BE: 212a
# UTF-16LE: 2a21
# UTF-32BE: 0000212a
# UTF-32LE: 2a210000

Returns:

  • (String)

    CLI-ready output



97
98
99
100
101
102
103
# File 'lib/unisec/hexdump.rb', line 97

def display
  "UTF-8: #{@utf8}\n" \
    "UTF-16BE: #{@utf16be}\n" \
    "UTF-16LE: #{@utf16le}\n" \
    "UTF-32BE: #{@utf32be}\n" \
    "UTF-32LE: #{@utf32le}"
end