class Origami::Filter::AHx

Class representing a filter used to encode and decode data written into hexadecimal.

Public Instance Methods

decode(string) click to toggle source

Decodes given data writen into upcase hexadecimal representation.

string

The data to decode.

# File lib/origami/filters/ascii.rb, line 48
def decode(string)
    input = string.include?(EOD) ? string[0...string.index(EOD)] : string
    digits = input.delete(" \f\t\r\n\0")

    # Ensure every digit is in the hexadecimal charset.
    unless digits =~ /^\h*$/
        digits = digits.match(/^\h*/).to_s

        raise InvalidASCIIHexStringError.new("Invalid characters", input_data: string, decoded_data: [ digits ].pack('H*'))
    end

    [ digits ].pack "H*"
end
encode(stream) click to toggle source

Encodes given data into upcase hexadecimal representation.

stream

The data to encode.

# File lib/origami/filters/ascii.rb, line 40
def encode(stream)
    stream.unpack("H*").join.upcase
end