Method: YubiRuby::HEX.encode

Defined in:
lib/hex.rb

.encode(obj) ⇒ Object

call-seq:

YubiRuby::HEX.encode("string") -> "hex string"

Encodes obj.to_str into an hex string.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hex.rb', line 48

def self.encode( obj )
  s = obj.to_str
  s.unpack('U'*s.length).collect {|x| tmp = x.to_s 16; tmp.length == 1 ? "0#{tmp}" : tmp }.join
rescue ArgumentError
  #non UTF-8 string
  s = obj.to_str
  out = []
  0.upto(s.length-1) do |i|
    tmp = s[i].to_s 16
    out << (tmp.length == 1 ? "0#{tmp}" : tmp )
  end
  out.join
end