在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型
1. 功能:
用来定义全局变量:locale(locale encoding)
头文件:
setlocale | <locale.h> | ANSI, Win 95, Win NT |
_wsetlocale | <locale.h> or <wchar.h> |
2. 原型:
char *setlocale( int category, const char *locale );
wchar_t *_wsetlocale( int category, const wchar_t *locale );
3. 参数:
1> category
指定全局变量locale会影响到的范围。宏和对应的影响范围如下:
LC_ALL
All categories, as listed below
LC_COLLATE
The strcoll, _stricoll, wcscoll, _wcsicoll, and strxfrm functions
LC_CTYPE
The character-handling functions (except isdigit, isxdigit, mbstowcs, and mbtowc, which are unaffected)
LC_MONETARY
Monetary-formatting information returned by the localeconv function
LC_NUMERIC
Decimal-point character for the formatted output routines (such as printf), for the data-conversion routines, and for the nonmonetary-formatting information returned by localeconv
LC_TIME
The strftime and wcsftime functions
2> locale
指定locale的名称
如果取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);
如果取值为“C”,则表示,所有的字符类型都用一字节来表示(取值小于256)
简体中文的locale名称为:"chs"
4. 返回值:
如果locale和category都正确的话,会返回与指定的locale和category相关的字符串;
如果locale或者category不正确的话,会返回一个null指针,目前的locale全局变量不会改变
示例代码:
获取本地所使用的语言:
void main() { char * localLanguage=setlocale(LC_ALL, ""); if(localLanguage==NULL) { printf("获取本地语言类型失败\n"); return; } printf("Locale Language is %s\n", localLanguage); }
Locale Language is Chinese_People's Republic of China.936