Index: constfold.c
===================================================================
--- constfold.c	(revision 767)
+++ constfold.c	(working copy)
@@ -25,6 +25,7 @@
 #include "expression.h"
 #include "aggregate.h"
 #include "declaration.h"
+#include "utf.h"
 
 #if __FreeBSD__
 #define fmodl fmod      // hack for now, fix later
@@ -1353,10 +1354,12 @@
 
     if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral))
     {   e = e2;
+        t = t1;
         goto L2;
     }
     else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull)
     {   e = e1;
+        t = t2;
      L2:
         Type *tn = e->type->toBasetype();
         if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar)
@@ -1365,11 +1368,13 @@
             void *s;
             StringExp *es;
             size_t len = 1;
-            int sz = tn->size();
+            if (t->nextOf())
+                t = t->nextOf()->toBasetype();
+            int sz = t->size();
             dinteger_t v = e->toInteger();
-
+            len = utf_codeLength(sz, v);
             s = mem.malloc((len + 1) * sz);
-            memcpy((unsigned char *)s, &v, sz);
+            utf_encode(sz, s, v);
 
             // Add terminating 0
             memset((unsigned char *)s + len * sz, 0, sz);
@@ -1459,13 +1464,13 @@
         StringExp *es1 = (StringExp *)e1;
         StringExp *es;
         Type *t;
-        size_t len = es1->len + 1;
         int sz = es1->sz;
         dinteger_t v = e2->toInteger();
 
+        size_t len = es1->len + utf_codeLength(sz, v);
         s = mem.malloc((len + 1) * sz);
         memcpy(s, es1->string, es1->len * sz);
-        memcpy((unsigned char *)s + es1->len * sz, &v, sz);
+        utf_encode(sz, (unsigned char *)s + (sz * es1->len), v);
 
         // Add terminating 0
         memset((unsigned char *)s + len * sz, 0, sz);
Index: utf.c
===================================================================
--- utf.c	(revision 767)
+++ utf.c	(working copy)
@@ -14,6 +14,7 @@
 #include <assert.h>
 
 #include "utf.h"
+#include "root.h"
 
 int utf_isValidDchar(dchar_t c)
 {
@@ -227,3 +228,93 @@
     return msg;
 }
 
+void utf_encodeChar(unsigned char *s, dchar_t c)
+{
+	if (c <= 0x7F)
+	{
+	    s[0] = (char) c;
+	}
+	else if (c <= 0x7FF)
+	{
+		s[0] = (char)(0xC0 | (c >> 6));
+		s[1] = (char)(0x80 | (c & 0x3F));
+	}
+	else if (c <= 0xFFFF)
+	{
+		s[0] = (char)(0xE0 | (c >> 12));
+		s[1] = (char)(0x80 | ((c >> 6) & 0x3F));
+		s[2] = (char)(0x80 | (c & 0x3F));
+	}
+    else if (c <= 0x10FFFF)
+    {
+		s[0] = (char)(0xF0 | (c >> 18));
+		s[1] = (char)(0x80 | ((c >> 12) & 0x3F));
+		s[2] = (char)(0x80 | ((c >> 6) & 0x3F));
+		s[3] = (char)(0x80 | (c & 0x3F));
+	}
+	else
+        assert(0);
+}
+
+void utf_encodeWchar(unsigned short *s, dchar_t c)
+{
+	if (c <= 0xFFFF)
+	{
+	    s[0] = (wchar_t) c;
+	}
+	else
+	{
+	    s[0] = (wchar_t) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800);
+	    s[1] = (wchar_t) (((c - 0x10000) & 0x3FF) + 0xDC00);
+    }
+}
+
+
+/**
+Returns the code length of c in the encoding.
+The code is returned in character count, not in bytes.
+ */
+
+int utf_codeLengthChar(dchar_t c)
+{
+    return
+        c <= 0x7F ? 1
+        : c <= 0x7FF ? 2
+        : c <= 0xFFFF ? 3
+        : c <= 0x10FFFF ? 4
+        : (assert(false), 6);
+}
+
+int utf_codeLengthWchar(dchar_t c)
+{
+    return c <= 0xFFFF ? 1 : 2;
+}
+
+/**
+Returns the code length of c in the encoding.
+sz is the encoding: 1 = utf8, 2 = utf16, 4 = utf32.
+The code is returned in character count, not in bytes.
+ */
+int utf_codeLength(int sz, dchar_t c)
+{
+    if (sz == 1)
+        return utf_codeLengthChar(c);
+    if (sz == 2)
+        return utf_codeLengthWchar(c);
+    assert(sz == 4);
+    return 1;
+}
+
+void utf_encode(int sz, void *s, dchar_t c)
+{
+    if (sz == 1)
+        utf_encodeChar((unsigned char *)s, c);
+    else if (sz == 2)
+        utf_encodeWchar((unsigned short *)s, c);
+    else
+    {
+        assert(sz == 4);
+        memcpy((unsigned char *)s, &c, sz);
+    }
+}
+
Index: utf.h
===================================================================
--- utf.h	(revision 767)
+++ utf.h	(working copy)
@@ -23,4 +23,13 @@
 
 extern int isUniAlpha(dchar_t);
 
+void utf_encodeChar(unsigned char *s, dchar_t c);
+void utf_encodeWchar(unsigned short *s, dchar_t c);
+
+int utf_codeLengthChar(dchar_t c);
+int utf_codeLengthWchar(dchar_t c);
+
+int utf_codeLength(int sz, dchar_t c);
+void utf_encode(int sz, void *s, dchar_t c);
+
 #endif
