title: More elegant, simpler way to convert code point to UTF-8
body: <p>For <a href="https://stackoverflow.com/q/7983574/405017">this question</a> I created the following Lua code that converts a Unicode code point to a UTF-8 character string. Is there a better way to do this (in Lua 5.1+)? "Better" in this case means <em>"drastically more efficient, or—preferably—far fewer lines of code"</em>.</p>
  
  <p>Note: I'm not really asking for a <a href="http://codereview.stackexchange.com">code review</a> of this algorithm; I'm asking for a better algorithm (or built-in library).</p>
  
  <pre class="lang-lua prettyprint-override"><code>do
    local bytebits = {
      {0x7F,{0,128}},
      {0x7FF,{192,32},{128,64}},
      {0xFFFF,{224,16},{128,64},{128,64}},
      {0x1FFFFF,{240,8},{128,64},{128,64},{128,64}}
    }
    function utf8(decimal)
      local charbytes = {}
      for b,lim in ipairs(bytebits) do
        if decimal&lt;=lim[1] then
          for i=b,1,-1 do
            local prefix,max = lim[i+1][1],lim[i+1][2]
            local mod = decimal % max
            charbytes[i] = string.char( prefix + mod )
            decimal = ( decimal - mod ) / max
          end
          break
        end
      end
      return table.concat(charbytes)
    end
  end
  
  c=utf8(0x24)     print(c.." is "..#c.." bytes.") --&gt; $ is 1 bytes.
  c=utf8(0xA2)     print(c.." is "..#c.." bytes.") --&gt; ¢ is 2 bytes.
  c=utf8(0x20AC)   print(c.." is "..#c.." bytes.") --&gt; € is 3 bytes.  
  c=utf8(0xFFFF)   print(c.." is "..#c.." bytes.") --&gt;  is 3 bytes.
  c=utf8(0x10000)  print(c.." is "..#c.." bytes.") --&gt;  is 4 bytes.
  c=utf8(0x24B62)  print(c.." is "..#c.." bytes.") --&gt;  is 4 bytes.   
  </code></pre>
  
  <hr>
  
  <p><em>I feel like there ought to be a way to get rid of the whole <code>bytebits</code> predefined table and loop just to find the matching entry. Looping from the back I could continually <code>%64</code> and add <code>128</code> to form the continuation bytes until the value was below 128, but I can't figure out how to elegantly generate the <code>0</code>/<code>110</code>/<code>1110</code>/<code>11110</code> preamble to add on.</em></p>
  
  <hr>
  
  <p><strong>Edit</strong>: Here's a slightly better reworking, with a speed optimization. This is not an acceptable answer, though, since the algorithm is still basically the same idea and about the same amount of code.</p>
  
  <pre><code>do
    local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} }
    function utf8(decimal)
      if decimal&lt;128 then return string.char(decimal) end
      local charbytes = {}
      for bytes,vals in ipairs(bytemarkers) do
        if decimal&lt;=vals[1] then
          for b=bytes+1,2,-1 do
            local mod = decimal%64
            decimal = (decimal-mod)/64
            charbytes[b] = string.char(128+mod)
          end
          charbytes[1] = string.char(vals[2]+decimal)
          break
        end
      end
      return table.concat(charbytes)
    end
  end
  </code></pre>