Question: Context
-------

Debian 64bits
Trying to write a int, eg 233 to a file and make it text print "é".

Question
--------

I cannot understand how i could write a utf8 equivalent char, eg "é" or any UTF-8 char largely wider than a char type can contain. The file should be human readable to send it over the network.

My goal is to write an int to a file and get its utf8 equivalent.

I don't know what I am doing.

Code
----

```
FILE * dd = fopen("/myfile.txt","w");
fprintf(dd, "%s", 233); /* The file should print "é" */
fclose(dd);

```

Thanks

UPDATE :

According to Biffen comment, here is another pices of code which writes "E9" (the hexadecimal value of "é");

```
int p = 233;
char r[5];
sprintf(r,"%x",p);
printf("%s\n",r);
fwrite(r,1,strlen(r),dd);
fclose(dd);

```

How to convert it to "é" ?

Update final working code :
===========================

```
UFILE * dd = u_fopen("/myfile.txt","wb", NULL, NULL);
UChar32 c = 233;
u_fputc(c,dd);
u_fclose(dd);

```

Answer: 
You seem to expect `printf()` to know about UTF-8, which it doesn't.

You can implement UTF-8 encoding yourself, it's a very simple encoding after all.

A solution might look like this:

```
void put_utf8(FILE *f, uint32_t codepoint)
{
    if (codepoint <= 0x7f) {
       fprintf(f, "%c", (char) codepoint & 0x7f);
    }
    else if (codepoint <= 0x7ff) {
       fprintf(f, "%c%c", (char) (0xc0 | (codepoint >> 6)),
                          (char) (0x80 | (codepoint & 0x3f));
    }
    else if (codepoint <= 0xffff) {
       fprintf(f, "%c%c%c", (char) (0xe0 | (codepoint >> 12)),
                            (char) (0x80 | ((codepoint >> 6) & 0x3f),
                            (char) (0x80 | (codepoint & 0x3f));
    }
    else if (codepoint <= 0x1fffff) {
       fprintf(f, "%c%c%c%c", (char) (0xf0 | (codepoint >> 18)),
                              (char) (0x80 | ((codepoint >> 12) & 0x3f),
                              (char) (0x80 | ((codepoint >> 6) & 0x3f),
                              (char) (0x80 | (codepoint & 0x3f));
    }
    else {
        // invalid codepoint
    }
}

```

You'd use this like so:

```
FILE *f = fopen("mytext.txt", "wb");
put_utf8(f, 233);
fclose(f);

```

and it will then output the two characters 0xC3 and 0xA9 to `f`.

See [Wikipedia](http://en.wikipedia.org/wiki/UTF-8#Description) for more details on UTF-8.
You could install the `libunistring-dev` package for [GNU libunistring](http://ftp.gnu.org/gnu/libunistring/), then include `<unistr.h>` and use e.g. `u32_to_u8` function to convert an UCS-4 string to an UTF-8 string. See [libunistring documentation](https://www.gnu.org/software/libunistring/manual/html_node/index.html#Top). Perhaps use [`<unistdio.h>`](https://www.gnu.org/software/libunistring/manual/html_node/unistdio_002eh.html#unistdio_002eh)