Skip to content

Instantly share code, notes, and snippets.

@atoulme
Created April 11, 2011 19:47
Show Gist options
  • Save atoulme/914171 to your computer and use it in GitHub Desktop.
Save atoulme/914171 to your computer and use it in GitHub Desktop.
A script to escape a String into unicode characters for double bytes strings
#!/usr/bin/env ruby
puts "Enter content and press Ctrl+D when ready"
old_stty = `stty -g`
# Set up the terminal in non-canonical mode input processing
# This causes the terminal to process one character at a time
system "stty -icanon min 1 time 0 -isig"
answer = ""
while true
char = STDIN.getc
break if char == ?\C-d # break on Ctrl-d
answer += char.chr
end
puts "\n\nOutput:"
puts answer.unpack('U*').map{ |i| i < 128 ? i.chr : '\u' + i.to_s(16).rjust(4, '0') }.join
system "stty #{old_stty}" # restore stty settings
@atoulme
Copy link
Author

atoulme commented Apr 11, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment