Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Created June 9, 2011 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coldnebo/1017423 to your computer and use it in GitHub Desktop.
Save coldnebo/1017423 to your computer and use it in GitHub Desktop.
ruby 1.9.2 character encoding helper
class String
def hexbytes
self.bytes.to_a.map {|c| c.to_s(16).upcase}.join
end
def hexchars
self.chars.to_a.map {|c| c.hexbytes}
end
def unicodechars
self.chars.to_a.map {|c| c.hexbytes}
end
def debug
puts "%-8s: %s" % ["encoding", encoding.name]
puts "%-8s: %-4s" % ["chars", to_series(chars.to_a)]
puts "%-8s: %-4s" % ["hex", to_series(hexchars)]
end
def to_series(array)
array.map {|c| c.rjust(4)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment