Module: EncodingTranslation

Included in:
Password
Defined in:
lib/encoding_translation.rb

Overview

EncodingTranslation Module

This module is a set of encoding translations shortcut methods using the String#pack and String#unpack methods.

Instance Method Summary collapse

Instance Method Details

#b64_to_bin(str) ⇒ Object

Converts a base64 string str to binary



50
51
52
# File 'lib/encoding_translation.rb', line 50

def b64_to_bin( str )
  str.unpack("m").at(0)
end

#bin_to_b64(str) ⇒ Object

Converts a binary string str to base 64



43
44
45
# File 'lib/encoding_translation.rb', line 43

def bin_to_b64( str )
  [str].pack('m').gsub(/\s+/, '')
end

#bin_to_hex(str) ⇒ Object

Converts a binary string str to hexidecimal



29
30
31
# File 'lib/encoding_translation.rb', line 29

def bin_to_hex( str )
  str.unpack("H#{str.length*2}").at(0)
end

#hex_to_bin(str) ⇒ Object

Converts a hexidecimal string str to binary



36
37
38
# File 'lib/encoding_translation.rb', line 36

def hex_to_bin( str )
  [str].pack("H#{str.length}")
end