# A Code Point's Tale: There and Back Again

**URL:** <https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576>\
**Category:** ruby-talk\
**Created:** [30 April 2011 04:12 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576 "2011-04-30T04:12:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terry\_Michaels](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Terry\_Michaels](https://rubytalk.org/u/Terry_Michaels)\
**Post date:** [30 April 2011 04:12 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/1 "2011-04-30T04:12:21Z")

</div>

This is probably obvious in the docs and I'm just missing it, but here  
goes: So, I see there is str.each\_codepoint, which I want to use in a  
function to convert Unicode Strings to a list of Unicode code points.  
But what can I do if I have a list of Unicode code points and want to  
convert them back into a String?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![xcr\_xcr](https://avatars.discourse-cdn.com/v4/letter/x/f9ae1b/32.png) [@xcr\_xcr](https://rubytalk.org/u/xcr_xcr)\
**Post date:** [30 April 2011 04:34 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/2 "2011-04-30T04:34:10Z")

</div>

I hope this is what u r looking for  
[http://ruby-unicode.rubyforge.org/doc/](http://ruby-unicode.rubyforge.org/doc/)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Markus\_Fischer](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Markus\_Fischer](https://rubytalk.org/u/Markus_Fischer)\
**Post date:** [30 April 2011 08:59 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/3 "2011-04-30T08:59:33Z")

</div>

Hi,

> This is probably obvious in the docs and I'm just missing it, but here  
> goes: So, I see there is str.each\_codepoint, which I want to use in a  
> function to convert Unicode Strings to a list of Unicode code points.  
> But what can I do if I have a list of Unicode code points and want to  
> convert them back into a String?

I think you can use Array#pack for that:

$ irb  
ruby-1.9.2-p180 :001 \> "f뀀oöbß".each\_codepoint.to\_a  
=\> [102, 45056, 111, 246, 98, 223]  
ruby-1.9.2-p180 :002 \> "f뀀oöbß".each\_codepoint.to\_a.pack("U\*")  
=\> "f뀀oöbß"

cheers

> **···**
>
> On 30.04.2011 06:12, Terry Michaels wrote:

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [1 May 2011 02:24 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/4 "2011-05-01T02:24:54Z")

</div>

Terry Michaels wrote in post #995906:

> This is probably obvious in the docs and I'm just missing it, but here  
> goes: So, I see there is str.each\_codepoint, which I want to use in a  
> function to convert Unicode Strings to a list of Unicode code points.  
> But what can I do if I have a list of Unicode code points and want to  
> convert them back into a String?

#encoding: UTF-8  
#That comment tells ruby to treat string literals in my source code,  
like  
#the one below, as utf-8 encoded.

str = "\xE2\x82\xAC\xE2\x82\xAC"

codes = str.each\_codepoint.to\_a

p codes  
puts codes.map {|code| code.chr(Encoding::UTF\_8) }.join(" ")

--output:--  
[8364, 8364]  
€ €

(You should see two euro symbols as the last line of output.)

I don't know where you are getting your string, but you can always do  
this:

str = "\xE2\x82\xAC\xE2\x82\xAC"  
str.force\_encoding("UTF-8")

codes = str.each\_codepoint.to\_a

p codes  
puts codes.map {|code| code.chr(Encoding::UTF\_8) }.join(" ")

--output:--  
[8364, 8364]  
€ €

(You should see two euro symbols as the last line of output.)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [1 May 2011 03:22 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/5 "2011-05-01T03:22:13Z")

</div>

Maybe each\_char() will work for you? Take a look at the following code.

str = "\xE2\x82\xAC\xE2\x82\xAC"  
puts str.encoding

str.force\_encoding("UTF-8")  
puts str.encoding

chars = str.each\_char.to\_a  
p chars

puts chars[0].encoding

puts chars.join

--output:--  
ASCII-8BIT  
UTF-8  
["\u20AC", "\u20AC"]  
UTF-8  
€€

(You should see two euro symbols as the last line of output.)

The output implies that a string with unicode escapes is given a UTF-8  
encoding by default. And that seems to be the case:

str = "\u20AC\u20AC"  
puts str.encoding

--output:--  
UTF-8

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [1 May 2011 02:41 UTC](https://rubytalk.org/t/a-code-points-tale-there-and-back-again/62576/6 "2011-05-01T02:41:27Z")

</div>

7stud -- wrote in post #996022:

> Terry Michaels wrote in post #995906:
> 
> > This is probably obvious in the docs and I'm just missing it,
> 
> You will never learn ruby unicode by reading the docs. Head over to  
> James Edward Gray II's website for some lessons:
> 
> [Gray Soft / Not Found](http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings)

Someone else blogged in great detail about all the intricacies of ruby  
unicode and its problems, but I can't find the link now.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
