C Program implements Unicode character representable utf-8 characters

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.