2010年8月30日 星期一

C語言印出中文,wchar_t

C語言印出中文,wchar_t

範例:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

void main (void)
{
wchar_t myString2[16] = { 0x55E8, 0x3001, 0x4F60, 0x597D};

setlocale(LC_ALL, "");
wprintf(L"This is a long string: %ls \n",myString2);
return;
}


結果:
This is a long string: 嗨、你好



code裡面的字碼是在Windows下用charmap查的,
當然,也可以透過下面的function來逆向查詢。
不過,只能在中文版的Linux下秀
不是說Unicode碼能在任何環境下使用嗎@@?
google了一下字型與Unicode和UTF-8之間的關係,還不是很明白啊




其它補充:

Ubuntu9.10中文板的locale
LANG=zh_TW.UTF-8
LANGUAGE=zh_TW:zh
LC_CTYPE="zh_TW.UTF-8"
LC_NUMERIC="zh_TW.UTF-8"
LC_TIME="zh_TW.UTF-8"
LC_COLLATE="zh_TW.UTF-8"
LC_MONETARY="zh_TW.UTF-8"
LC_MESSAGES="zh_TW.UTF-8"
LC_PAPER="zh_TW.UTF-8"
LC_NAME="zh_TW.UTF-8"
LC_ADDRESS="zh_TW.UTF-8"
LC_TELEPHONE="zh_TW.UTF-8"
LC_MEASUREMENT="zh_TW.UTF-8"
LC_IDENTIFICATION="zh_TW.UTF-8"
LC_ALL=


Ubuntu9.10英文板的locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=



【轉貼:Here】char 轉 wchar_t
wchar_t *char_string_to_wchar_t(const char *src,const char *encoding)
{
if(setlocale(LC_CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n");
return NULL;
}
size_t n = mbstowcs(NULL, src, 0);
wchar_t *dst = malloc((n + 1) * sizeof *dst);
if(!dst)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(mbstowcs(dst, src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}



【轉貼:Here】wchar_t 轉 char
char *wchar_t_string_to_char(const wchar_t *src, const char *encoding)
{
if(setlocale(LC_CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n");
return NULL;
}
size_t n = wcstombs(NULL, src, 0);
char *dst = malloc(n + 1);
if(dst == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(wcstombs(dst, src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}




中文字查字碼 (只找了幾個字test過)
void test2 (void)
{
wchar_t wstr[]=L"嗨,你好";
const char *encoding = "zh_TW.UTF-8";
unsigned char *utf8;
unsigned char utf8_cp[128];
wchar_t *utf8touni;
wchar_t *pp;

utf8=wchar_t_string_to_char(wstr, encoding);
strcpy(utf8_cp, utf8);
utf8touni=char_string_to_wchar_t(utf8_cp, encoding);

for(pp = utf8touni; *pp; pp++)
printf("0x%X ", (unsigned) *pp);

printf("\n");

free(utf8);
free(utf8touni);
return;
}


ref:
how to convert wchar* to char*

沒有留言:

張貼留言