Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created January 27, 2009 11: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 hipertracker/53311 to your computer and use it in GitHub Desktop.
Save hipertracker/53311 to your computer and use it in GitHub Desktop.
# z wątku http://www.rubyonrails.pl/forum/t112-Zamiana-pojedynczych-znakow
require 'iconv'
def pl2ascii(s)
ascii = "acelnoszzACELNOSZZ"
cep = "\271\346\352\263\361\363\234\277\237\245\306\312\243\321\323\214\257\217"
s = Iconv.new("cp1250", "UTF-8").iconv(s)
s.tr!(cep,ascii)
end
puts pl2ascii('zażółć gęślą jaźń ZAŻÓŁĆ GĘŚLĄ JAŹŃ') # => zazolc gesla jazn ZAZOLC GESLA JAZN
=begin
Do generacji stringa z kodami cep1250 użyłem Pythona:
pl = [oct(ord(c)) for c in unicode('ąćęłńóśżźĄĆĘŁŃÓŚŻŹ', 'utf8').encode('cp1250')]
print ''.join(["\\%s" % c[1:] for c in pl])
# => \271\346\352\263\361\363\234\277\237\245\306\312\243\321\323\214\257\217
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment