Question: I have a rails server running, where I have a bunch of clocks emojis, and I want to render them to the HTML. The emojis are in ASCII format:

```
ch = "\xF0\x9F\x95\x8f" ; 12.times.map { ch.next!.dup }.rotate(-1)

# => ["", "", "", "", "", "", "", "", "", "", "", ""]

```

What I want is this:

```
> String.define_method(:to_html_utf8) { chars.map! { |x| "&#x#{x.dump[3..-2].delete('{}')};" }.join }

> ch = "\xF0\x9F\x95\x8f" ; 12.times.map { ch.next!.to_html_utf8 }.rotate(-1)
# => ["&#x1F55B;", "&#x1F550;", "&#x1F551;", "&#x1F552;", "&#x1F553;", "&#x1F554;", "&#x1F555;", "&#x1F556;", "&#x1F557;", "&#x1F558;", "&#x1F559;", "&#x1F55A;"]

> ?.to_html_utf8
# => "&#x1F584;"

> "".to_html_utf8
#=> "&#x1F42D;&#x1F439;"

```

As you can see the to\_html\_utf8 does use some brute force way to get the job done.
Is there a better way to convert the emojis in aforementioned html compatible UTF-8?

Please note that it would be better to avoid and rails helpers or rails stuff in general, and it can be run with ruby 2.7+ only using standard library stuff.

Answer: 
> 
> The emojis are in ASCII format:
> 
> 
> 
> ```
> ch = "\xF0\x9F\x95\x8f"
> 
> ```
> 
> 

`0xf0 0x9f 0x95 0x8f` is the character's UTF-8 *byte sequence*. Don't use that unless you absolutely have to. It's much easier to enter the emojis directly, e.g.:

```
ch = ''
#=> ""

```

or to use the character's *codepoint*, e.g.:

```
ch = "\u{1f550}"
#=> ""

ch = 0x1f550.chr('UTF-8')
#=> ""

```

You can usually just render that character into your HTML page if the "charset" is UTF-8.

If you want to turn the string's characters into their *numeric character reference* counterparts yourself, you could use:

```
ch.codepoints.map { |cp| format('&#x%x;', cp) }.join
#=> "&#x1f550;"

```

Note that the conversion is trivial – `1f550` is simply the character's (hex) codepoint.
The easiest way is to simply use UTF-8 natively and not escaping anything.