Search code examples
ruby-on-railsrubyunicodeutf-8ucs2

Convert string to two byte hex unicode in Ruby on Rails


I'm translating messages using Bing Translator and sending the results via SMS text message. The resulting strings often contain non-English characters, e.g. Korean, Japanese, Greek.

I am using the Clickatell SMS Gateway and according to the Clickatell spec here: http://www.clickatell.com/downloads/http/Clickatell_HTTP.pdf ...I think I should encode my strings in two-byte Unicode, hex-encoded.

For example, the Greek characters:

ΩΨΘ

After conversion should become:

03A903A80398

Which is then added to the querystring in my HTTP get request.

My issue however is finding the syntax to to this succinctly in my Ruby on Rails app.


Solution

  • I like fun ones like these. :) Try this:

    input.codepoints.map { |c| "%04X" % c }.join
    

    What I see:

    [1] pry(main)> x = "\u03A9\u03A8\u0398"
    => "ΩΨΘ"
    [2] pry(main)> x.codepoints.map { |c| "%04X" % c }.join
    => "03A903A80398"