# Integer#chr

**URL:** <https://rubytalk.org/t/integer-chr/75069>\
**Category:** ruby-talk\
**Created:** [24 August 2019 18:01 UTC](https://rubytalk.org/t/integer-chr/75069 "2019-08-24T18:01:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_Wagner](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@Detlef\_Wagner](https://rubytalk.org/u/Detlef_Wagner)\
**Post date:** [24 August 2019 18:01 UTC](https://rubytalk.org/t/integer-chr/75069/1 "2019-08-24T18:01:51Z")

</div>

Hi,

i have a problem with Integer#chr, where the encoding is UTF-8. If I call

0xC384.chr(Encoding::UTF\_8)

in irb, I expect the German umlaut Ä, but I get 쎄, where I even can't  
say what language it is 😉

Is it a bug, or do I something wrong?

Cheers, detlef

ruby version ruby 2.5.5p157 (2019-03-15 revision 67260) [x86\_64-linux-gnu]  
irb version 0.9.6(09/06/30)

---

<div class="post-metadata">

**Author:** ![Skip\_Gibson](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Skip\_Gibson](https://rubytalk.org/u/Skip_Gibson)\
**Post date:** [24 August 2019 18:18 UTC](https://rubytalk.org/t/integer-chr/75069/2 "2019-08-24T18:18:26Z")

</div>

I think you’ve just used the wrong unicode number, this should work

0x00C4.chr(Encoding::UTF\_8)

Hope that helps,  
Skip

> **···**
>
> On 24 August 2019 at 19:02:35, Detlef Wagner ([detlef.wagner@gmx.org](mailto:detlef.wagner@gmx.org)) wrote:
> 
> Hi,
> 
> i have a problem with Integer#chr, where the encoding is UTF-8. If I call
> 
> 0xC384.chr(Encoding::UTF\_8)
> 
> in irb, I expect the German umlaut Ä, but I get 쎄, where I even can't  
> say what language it is 😉
> 
> Is it a bug, or do I something wrong?
> 
> Cheers, detlef
> 
> ruby version ruby 2.5.5p157 (2019-03-15 revision 67260) [x86\_64-linux-gnu]  
> irb version 0.9.6(09/06/30)

---

<div class="post-metadata">

**Author:** ![Jorg\_W\_Mittag3](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Jorg\_W\_Mittag3](https://rubytalk.org/u/Jorg_W_Mittag3)\
**Post date:** [25 August 2019 14:20 UTC](https://rubytalk.org/t/integer-chr/75069/3 "2019-08-25T14:20:46Z")

</div>

Hi Detlef,

> i have a problem with Integer#chr, where the encoding is UTF-8. If I call
> 
> 0xC384.chr(Encoding::UTF\_8)
> 
> in irb, I expect the German umlaut Ä, but I get 쎄, where I even can't  
> say what language it is 😉
> 
> Is it a bug, or do I something wrong?

This is not a bug, this is exactly the expected behavior. If you look  
up U+C384 in the Unicode Character Database, you will find:

[https://unicode.org/cldr/utility/character.jsp?a=C384](https://unicode.org/cldr/utility/character.jsp?a=C384)  
쎄  
C384  
HANGUL SYLLABLE SSE  
Hangul Script

So, in other words, you get exactly the character you asked for.

Note that Unicode was explicitly designed such that ASCII characters  
have the same code point in Unicode as they have in ASCII, which means  
that Latin letters and arabic numbers without diacritics generally  
have the lowest code points. After that, come most other European  
characters. So, the very high codepoint (50052) should already have  
tipped you off that this can't be a European character.

The German Umlaut Ä (Latin capital letter A with diaeresis) can be  
formed in Unicode in two different ways:

As the single pre-composed character U+00C4:

[https://unicode.org/cldr/utility/character.jsp?a=00C4](https://unicode.org/cldr/utility/character.jsp?a=00C4)  
Ä  
00C4  
LATIN CAPITAL LETTER A WITH DIAERESIS  
Latin Script

or as the combination of the two characters U+0041 Latin capital  
letter A and U+0308 Combining Diaeresis:

[https://unicode.org/cldr/utility/character.jsp?a=0041](https://unicode.org/cldr/utility/character.jsp?a=0041)  
A  
0041  
LATIN CAPITAL LETTER A  
Latin Script

followed by

[https://unicode.org/cldr/utility/character.jsp?a=0308](https://unicode.org/cldr/utility/character.jsp?a=0308)  
̈  
0308  
COMBINING DIAERESIS  
Nonspacing Mark

The different encodings have different advantages and disadvantages.  
For example, the pre-composed encoding is closer to how a pre-Unicode  
encoding in e.g. ISO8859-1 would work. The de-composed encoding has  
the advantage that in UTF-8, characters in the ASCII character set  
encode to the same encoding as ASCII, which means that obsolete text  
processors that strip anything that is not 7-bit will not mangle the  
text quite as badly. E.g. the word "Bär", which in the de-composed  
form consists of the Unicode code points

U+0040 U+0061 U+0308 U+0072

will be encoded in UTF-8 as

\x40 \x61 \xCC\x88 \x72

when you process this with a broken text processor that strips  
non-7-bit characters, the result will be

\x40 \x61 \x72

which is "Bar", and when processed with a broken text processor that  
clears the 8th bit, the result will be

\x40 \x61 \x4C\x08 \x72

which is "BaL\<BEL\>r".

OTOH, the word "Bär" using a pre-composed Latin small letter a with  
diaeresis consist of the Unicode code points

U+0040 U+00E4 U+0072

which is encoded in UTF-8 as

\x40 \xC3\xA4 \x72

Which will be mangled by broken text processors into

\x40 \x72 "Br"

or

\x40 \x43\x24 \x72 "BC$r".

Which of the two encodings you prefer is largely a matter of context,  
and also at least a bit of taste.

Greetings,  
&nbsp;&nbsp;&nbsp;&nbsp;Jörg.

---

<div class="post-metadata">

**Author:** ![Kim\_Pedersen](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@Kim\_Pedersen](https://rubytalk.org/u/Kim_Pedersen)\
**Post date:** [25 August 2019 15:35 UTC](https://rubytalk.org/t/integer-chr/75069/4 "2019-08-25T15:35:24Z")

</div>

Hi

The german umlaut Ä has unicode number U+00C4, which in UTF-8 encoding  
becomes "\xC3\x84".

> puts "\u00c4" =\> Ä

> puts "\xc3\x84".force\_encoding 'UTF-8' =\> Ä

The number 0xc384 interpreted as a unicode point is a korean character.

Best regards

Kim

> **···**
>
> Den 24.08.2019 20:01, skrev Detlef Wagner:
> 
> > Hi,
> > 
> > i have a problem with Integer#chr, where the encoding is UTF-8. If I call
> > 
> > 0xC384.chr(Encoding::UTF\_8)
> > 
> > in irb, I expect the German umlaut Ä, but I get 쎄, where I even can't  
> > say what language it is 😉
> > 
> > Is it a bug, or do I something wrong?
> > 
> > Cheers, detlef
> > 
> > ruby version ruby 2.5.5p157 (2019-03-15 revision 67260) [x86\_64-linux-gnu]  
> > irb version 0.9.6(09/06/30)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
