# Convert \\uXXXX to character

**URL:** <https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771>\
**Category:** Ruby\
**Created:** [June 27, 2010, 2:33pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771 "2010-06-27T14:33:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 27, 2010, 2:33pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/1 "2010-06-27T14:33:24Z")

</div>

I have string: ‘\u041f\u0440\u0438\u0432\u0435\u0442!’ and i need to  
convert it to string such as ‘Ð¿Ñ€Ð¸Ð²ÐµÑ‚!’.  
I can convert string to ‘041f 0440 0438 0432 0435 0442’, then convert to  
decimal and at the end convert each code to character with function:

> str.scan(/[0-9]+/).each {|x| result\_str \<\< x.to\_i}

but i don’t think that it is the most rational way.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 27, 2010, 8:06pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/2 "2010-06-27T20:06:08Z")

</div>

I recommend you:  
[http://blog.grayproductions.net/articles/understanding\_m17n](http://blog.grayproductions.net/articles/understanding_m17n)

2010/6/27 born in USSR [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 27, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/3 "2010-06-27T20:38:55Z")

</div>

On 06/27/2010 05:33 AM, born in USSR wrote:

> I have string: ‘\u041f\u0440\u0438\u0432\u0435\u0442!’ and i need to  
> convert it to string such as ‘Ð¿Ñ€Ð¸Ð²ÐµÑ‚!’.  
> I can convert string to ‘041f 0440 0438 0432 0435 0442’, then convert to  
> decimal and at the end convert each code to character with function:
> 
> > str.scan(/[0-9]+/).each {|x| result\_str\<\< x.to\_i}
> 
> but i don’t think that it is the most rational way.

irb(main):001:0\> RUBY\_VERSION  
=\> “1.9.1”  
irb(main):002:0\> puts ‘\u041f\u0440\u0438\u0432\u0435\u0442!’  
\u041f\u0440\u0438\u0432\u0435\u0442!  
=\> nil  
irb(main):003:0\> puts “\u041f\u0440\u0438\u0432\u0435\u0442!”  
ÐŸÑ€Ð¸Ð²ÐµÑ‚!  
=\> nil

Note the difference in single quotes versus double quotes.

-Justin

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 28, 2010, 6:23am UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/4 "2010-06-28T06:23:04Z")

</div>

On Jun 27, 2010, at 8:33 AM, born in USSR wrote:

> I have string: ‘\u041f\u0440\u0438\u0432\u0435\u0442!’ and i need to  
> convert it to string such as ‘Ð¿Ñ€Ð¸Ð²ÐµÑ‚!’.  
> I can convert string to ‘041f 0440 0438 0432 0435 0442’, then convert to  
> decimal and at the end convert each code to character with function:

If I understand you correctly you can leverage Ruby’s parser to  
interpret your string literal:

irb\> x = ‘\u041f\u0440\u0438\u0432\u0435\u0442!’  
=\> “\u041f\u0440\u0438\u0432\u0435\u0442!”  
irb\> eval(""#{x}"")  
=\> “ÐŸÑ€Ð¸Ð²ÐµÑ‚!”

Be careful though with eval, make sure your string to be evaluated  
doesn’t contain any untrusted code.

Gary W.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 28, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/5 "2010-06-28T15:25:18Z")

</div>

On 28 June 2010 07:39, Markus S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> IMHO better than eval 😉

str = ‘\u041f\u0440\u0438\u0432\u0435\u0442!’  
p str.gsub(/\u(\h{4})/) {  
$1.to\_i(16).chr(‘UTF-8’)  
}

What do you say of this?  
Well, I was searching something in the line of String#unpack, like

p str.gsub(/\u(\h{4})/) {  
[$1.to\_i(16)].pack(‘U’)  
}

but as we are scanning one by one, it is not interesting and need an  
extra array like in JSON (but it is 1.8 compatible).

B.D.

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 28, 2010, 6:40am UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/6 "2010-06-28T06:40:03Z")

</div>

I think the JSON parser is able to decode this unicode escapes  
correctly!

The JSON parser will not decode an pure string to you have to wrap the  
string into array syntax, and extract after parsing:

mbj@mbj ~ $ irb  
irb(main):001:0\> require ‘json’  
=\> true  
irb(main):002:0\> x = ‘\u041f\u0440\u0438\u0432\u0435\u0442!’  
=\> “\u041f\u0440\u0438\u0432\u0435\u0442!”  
irb(main):003:0\> JSON.parse(’["’+x+’"]’)[0]  
=\> “ÐŸÑ€Ð¸Ð²ÐµÑ‚!”  
irb(main):004:0\>

IMHO better than eval 😉

---

<div class="post-metadata">

**Author:** ![born\_in\_USSR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/2bfe46/32.png) [@born\_in\_USSR](https://www.ruby-forum.com/u/born_in_USSR)\
**Post date:** [June 28, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/convert-uxxxx-to-character/190771/7 "2010-06-28T16:44:57Z")

</div>

On 28.06.2010 15:24, Benoit D. wrote:

> On 28 June 2010 07:39, Markus S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > IMHO better than eval 😉
> 
> str = ‘\u041f\u0440\u0438\u0432\u0435\u0442!’  
> p str.gsub(/\u(\h{4})/) {  
> $1.to\_i(16).chr(‘UTF-8’)  
> }

Donâ€™t forget that Unicode Code Points not only cover the BMP and can be  
up to 6 hex digits long  
[[Unicode - Wikipedia](http://en.wikipedia.org/wiki/Unicode#Character_planes_and_blocks)].

What do you do if the string contained some escaped backslashes, like in  
str = ‘\u041f\u0440’? Does it contain Surrogates?

â€“ Matthias
