linux 下的 wchar_t

来源:互联网 发布:unity3d 显示3d模型 编辑:程序博客网 时间:2024/06/05 09:29
 

linux 下的 wchar_t

分类: linux 4539人阅读 评论(0) 收藏 举报
linuxnullwindowsgcc

1. 默认情况下,windows 下的 wchar_t 占两个字节的长度,而 linux 下的 wchar_t 占四个字节的长度,可以在使用 gcc 编译程序的时候再后面跟上 -fshort-wchar 来解决这个问题。

2. linux 下 wchar_t* 字符串的输出问题 —— 没有解决。

3. 如下程序,可输出宽字符,但是如果加上 -fshort-wchar 编译选项,则输出为乱码。

[cpp] view plaincopy
  1. #include <wchar.h>     
  2. #include <locale.h>     
  3. #include <stdlib.h>     
  4. #include <stdio.h>     
  5. #include <string.h>      
  6. int main(void)      
  7. {      
  8.     setlocale(LC_ALL,"zh_CN.UTF-8");    
  9.     wchar_t a[10] = L"你好";  
  10.     wprintf(L"this is a test !/n");    
  11.     wprintf(L"%d/n",wcslen(a));      
  12.     wprintf(L"%ls/n",a);      
  13.     retur 0;  
  14. }  

4. 可以使用下面这个函数,输出经参数 -fshort-wchar 编译过的宽字符。

[cpp] view plaincopy
  1. void print_wcs( const wchar_t *text )  
  2. {  
  3.     int         len = 0;  
  4.     int      i   = 0;  
  5.     wchar_t  *p  = NULL;  
  6.       
  7.     if( NULL == text )  
  8.         return;  
  9.       
  10.     p = text;  
  11.     while( *p != L'/0' )  
  12.         printf( "%lc", *p++ );  

0 0
原创粉丝点击