[C/C++] ASCII to UTF8

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

 

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

你可能感兴趣的:(c,String)