[C/C++] ASCII to UTF8

來源:互聯網
上載者:User

將ASCII轉為UTF8的代碼.....

 

#include <stdio.h><br />#include <stdlib.h><br />unsigned char* ASCIItoUNICODE (unsigned char ch);<br />unsigned int* ConvertString (unsigned char *string);<br />void UnicodePrint(unsigned int* Message); </p><p>int main()<br />{<br /> unsigned char text[] = "Ieeaao,world" ;<br /> unsigned int *UnMess;<br /> printf("Content-type: text/plain/n/n/n"); </p><p> UnMess = ConvertString(text);<br /> UnicodePrint(UnMess); </p><p>} </p><p>unsigned char* ASCIItoUNICODE (unsigned char ch)<br />{<br />unsigned char Val[2];<br />if ((ch < 192)&&(ch != 168)&&(ch != 184)) {Val[0] = 0; Val[1] = ch; return Val;}<br />if (ch == 168) {Val[0] = 208; Val[1] = 129; return Val;}<br />if (ch == 184) {Val[0] = 209; Val[1] = 145; return Val;}<br />if (ch < 240) {Val[0] = 208; Val[1] = ch-48; return Val;}<br />if (ch < 249) {Val[0] = 209; Val[1] = ch-112; return Val;}<br />}<br />unsigned int* ConvertString (unsigned char *string)<br />{<br /> unsigned int size=0, *NewString;<br /> unsigned char* Uni;<br /> while (string[size++]!=0);<br /> NewString = (unsigned int*)malloc(sizeof(unsigned int)*2*size-1);<br /> NewString[0]=2*size-1;<br /> size=0;<br /> while (string[size]!=0)<br /> {<br /> Uni = ASCIItoUNICODE(string[size]);<br /> NewString[2*size+1]=Uni[0];<br /> NewString[2*size+2]=Uni[1];<br /> size++;<br /> }<br /> return NewString;<br />} </p><p>void UnicodePrint(unsigned int* Message)<br />{<br /> int size, i; </p><p> size=Message[0];<br /> if (Message[1] == 0) i=2;<br /> else i=1; </p><p> while (i<size)<br /> {<br /> printf("%C",Message[i]);<br /> i++;<br /> }<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.