# Number to UTF-conversion question

**URL:** <https://www.ruby-forum.com/t/number-to-utf-conversion-question/149805>\
**Category:** Rails\
**Created:** [September 30, 2008, 1:04am UTC](https://www.ruby-forum.com/t/number-to-utf-conversion-question/149805 "2008-09-30T01:04:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 30, 2008, 1:04am UTC](https://www.ruby-forum.com/t/number-to-utf-conversion-question/149805/1 "2008-09-30T01:04:10Z")

</div>

All, Sorry for cross-posting but I am stuck on this thing for a quite  
some time:

I have a variable x = 1046

How can I convert into UTF-8 character? x.chr does not work for it.

Basically I need to put x in a string as UTF-8 character to display on a  
page.

Regards,

- newB

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 30, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/number-to-utf-conversion-question/149805/2 "2008-09-30T15:01:45Z")

</div>

On Sep 29, 2008, at 7:04 PM, Simon Watson wrote:

> page.
> 
> Regards,
> 
> - newB

Do you mean to say that x holds a Unicode code point? If that’s the  
case (since ASCII is a subset of Unicode, x.to\_s =\> “1046” is  
trivial), then you can use something like this code I wrote a while  
back:  
irb\> (“U+”+(‘0’\*4+x.to\_s(16))[-4,4]).to\_utf8  
=\> “\320\226”

Of course, you could hide most of that in an Integer#to\_utf8 method.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

# -_- ruby -_-

class String

# For a string that matches /(?i:U+?|\u)?\d{4}/, return a

suitable UTF-8

# string for that character.

def to\_utf8  
case point = self.match(/[[:xdigit:]]{4}/)[0].to\_i(16)  
when 0…0x7f  
point.chr  
when 0x80…0x07ff  
x = point & 0b111111  
point \>\>= 6  
y = point  
“#{(0xC0 | y).chr}#{(0x80 | x).chr}”  
when 0x0800…0xFFFF  
x = point & 0b111111  
point \>\>= 6  
y = point & 0b111111  
point \>\>= 6  
z = point  
“#{(0xE0 | z).chr}#{(0x80 | y).chr}#{(0x80 | x).chr}”  
when 0x10000…0x10FFFF  
raise NotImplementedError, “UTF-8 four byte sequences not yet  
supported”  
else  
raise ArgumentError, “Values above U+10FFFF are not supported”  
end  
end  
end

if **FILE** == $0  
require ‘test/unit’  
class UnicodeHelperTest \< Test::Unit::TestCase  
def test\_ascii  
assert\_equal ‘!’, “U+0021”.to\_utf8, ‘EXCLAMATION MARK’  
assert\_equal ‘A’, “U+0041”.to\_utf8, ‘UPPERCASE LETTER A’  
assert\_equal ‘-’, “U+002D”.to\_utf8, ‘HYPHEN-MINUS’  
assert\_equal ‘~’, “U+007E”.to\_utf8, ‘TILDE’

```
   assert_equal '!', "0021".to_utf8, 'EXCLAMATION MARK'
   assert_equal 'A', "0041".to_utf8, 'UPPERCASE LETTER A'
   assert_equal '-', "002D".to_utf8, 'HYPHEN-MINUS'
   assert_equal '~', "007E".to_utf8, 'TILDE'

   assert_equal '!', "\\u0021".to_utf8, 'EXCLAMATION MARK'
   assert_equal 'A', "\\u0041".to_utf8, 'UPPERCASE LETTER A'
   assert_equal '-', "\\u002D".to_utf8, 'HYPHEN-MINUS'
   assert_equal '~', "\\u007E".to_utf8, 'TILDE'
 end
 def test_hi_bit_ascii
   assert_equal "\xC2\x80", "U+0080".to_utf8, "C-cedilla"
   assert_equal "\xC2\xA4", "U+00A4".to_utf8, "Spanish n-tilde"
 end
 def test_general_punctuation
   assert_equal "\342\200\220", "U+2010".to_utf8, "HYPHEN"
   assert_equal "\342\200\221", "U+2011".to_utf8, "NON-BREAKING

```

HYPHEN"  
assert\_equal “\342\200\222”, “U+2012”.to\_utf8, “FIGURE DASH”  
assert\_equal “\342\200\223”, “U+2013”.to\_utf8, “EN DASH”  
assert\_equal “\342\200\224”, “U+2014”.to\_utf8, “EM DASH”  
assert\_equal “\342\200\225”, “U+2015”.to\_utf8, “QUOTATION DASH”  
end  
end  
end  
**END**

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [September 30, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/number-to-utf-conversion-question/149805/3 "2008-09-30T15:17:01Z")

</div>

On 30 Sep 2008, at 14:00, Rob B. wrote:

> > 
> 
> Do you mean to say that x holds a Unicode code point? If that’s the  
> case (since ASCII is a subset of Unicode, x.to\_s =\> “1046” is  
> trivial), then you can use something like this code I wrote a while  
> back:  
> irb\> (“U+”+(‘0’\*4+x.to\_s(16))[-4,4]).to\_utf8  
> =\> “\320\226”

Depend on what you are doing,

[1046].pack(‘U’)

may also be appropriate.

Fred
