Module: Ronin::Support::Encoding::Base16

Defined in:
lib/ronin/support/encoding/base16.rb

Overview

Base16 encoding.

Core-Ext Methods

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ String

Base16 decodes the String.

Examples:

Encoding::Base16.decode("68656C6C6F")
# => "hello"

Parameters:

  • data (String)

    The given data to Base16 decode.

Returns:

  • (String)

    The Base16 decoded version of the String.

Since:

  • 1.0.0



73
74
75
76
77
78
79
80
81
# File 'lib/ronin/support/encoding/base16.rb', line 73

def self.decode(data)
  decoded = String.new(encoding: Encoding::ASCII_8BIT)

  data.scan(/../).each do |hex_char|
    decoded << hex_char.to_i(16).chr
  end

  return decoded
end

.encode(data) ⇒ String

Base16 encodes the given data.

Examples:

Encoding::Base16.encode("hello")
# => "68656C6C6F"

Parameters:

  • data (String)

    The given data to Base16 encode.

Returns:

  • (String)

    The Base16 encoded version of the String.

Since:

  • 1.0.0



50
51
52
53
54
55
56
57
58
# File 'lib/ronin/support/encoding/base16.rb', line 50

def self.encode(data)
  encoded = String.new

  data.each_byte do |byte|
    encoded << ("%.2x" % byte)
  end

  return encoded
end