Class: Origami::Filter::ASCIIHex

Inherits:
Object
  • Object
show all
Includes:
Origami::Filter
Defined in:
lib/origami/filters/ascii.rb

Overview

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

Constant Summary collapse

EOD =

:nodoc:

">"

Instance Method Summary collapse

Instance Method Details

#decode(string) ⇒ Object

Decodes given data writen into upcase hexadecimal representation.

string

The data to decode.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/origami/filters/ascii.rb', line 53

def decode(string)
  
  input = string.include?(?>) ? string[0..string.index(?>) - 1] : string
  digits = input.delete(" \f\t\r\n\0").split(/(..)/).delete_if{|digit| digit.empty?}
  
  if not digits.all? { |d| d =~ /[a-fA-F0-9]{1,2}/ }
    raise InvalidASCIIHexStringError, input
  end
  
  digits.pack("H2" * digits.size)
end

#encode(stream) ⇒ Object

Encodes given data into upcase hexadecimal representation.

stream

The data to encode.



45
46
47
# File 'lib/origami/filters/ascii.rb', line 45

def encode(stream)
  stream.unpack("H2" * stream.size).join.upcase
end