The following is a C program implementation that converts a Unicode character to Utf-8:
/** =====================================================================================** filename:utou.c** Description: * * version:1.0* created:08/06/2015 10:53:31 am* revision:none* compiler:gcc** author:your NAME (), * ORG Anization: * * =====================================================================================*/#include<stdlib.h>#include<stdio.h>#include<string.h>intUtou (unsignedintCodepoint,Char*str) {Char out[4]; if(Codepoint <0x80) { out[0] = (Char) codepoint; strncpy (str, out,1); }Else if(Codepoint <0x800) { out[0] =0xC0| (Codepoint >>6); out[1] =0x80| (Codepoint &0x0000003F); strncpy (str, out,2); }Else if(Codepoint <0x10000) { out[0] =0xE0| (Codepoint >> A); out[1] =0x80| ((Codepoint &0x00000fff) >>6); out[2] =0x80| (Codepoint &0x0000003F); strncpy (str, out,3); }Else{ out[0] =0xF0| (Codepoint >> -); out[1] =0x80| ((Codepoint &0x0003ffff) >> A); out[2] =0x80| ((Codepoint &0x00000fff) >>6); out[3] =0x80| (Codepoint &0x0000003F); strncpy (str, out,4); }return 0; }/** = = FUNCTION ======================================================================* name:main* Description: * =====================================================================================*/intMain (intargcChar*argv[]) {//unsigned int codepoint = 0x6c41;UnsignedintCodepoint =0xFFFF; Charstr[ -] = {}; Utou (codepoint, str); printf ("str:%s\n", str); returnexit_success;} /*----------End of function main----------*/
C Program implements Unicode character representable utf-8 characters