setlocale与编码转换那些事

最近项目上调查printf语句不能正常格式化字符串的问题,做下总结。

 

以sprintf_s函数来说明问题的现象。

 

int sprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format [,
      argument] ... 
);
View Code

问题发生的条件

  1. 使用了setlocale
  2. format参数是utf8编码
  3. format的%是非ASC字符,已locale编码来解释的话,刚好%与前面的编码结合,被当初了一个新的文字。

例如我们出问题时,locale是".ACP",也就是ANSI(CP932),format参数是"日付%04d/%02d/%02d 時刻%02d:%02d:%02d.%03d",UTF编码如下:

00000003h: E6 97 A5 E4 BB 98 25 30 34 64 2F 25 30 32 64 2F ; 譌・莉・04d/%02d/
00000013h: 25 30 32 64 20 E6 99 82 E5 88 BB 25 30 32 64 3A ; %02d 譎ょ綾%02d:
00000023h: 25 30 32 64 3A 25 30 32 64 2E 25 30 33 64 ; %02d:%02d.%03d

其中日付对应的UTF8编码是

E6 97 A5 E4 BB 98

CP932环境下E6 97是一个独立的SJIS双字节文字【譌】。A5是一个单字节文字【・】。E4 BB是一个双字节文字【莉】,

98 与 25刚好满足SJIS双字节文字的编码要求,首字节在0x81~0x9f或者0xe0~0xef之间。所以对应print来说,第一个%s已经不存在了,自然不能被替换。

 

下面就来说说setlocale。

 

  • 影响的函数

依据https://msdn.microsoft.com/en-US/library/x99tb11d(v=vs.80).aspx,setlocale会根据设定的category不同,产生不同的影响。

LC_COLLATE

The strcoll_stricollwcscoll_wcsicollstrxfrm_strncoll_strnicoll_wcsncoll_wcsnicoll, and wcsxfrm functions.

LC_TIME

The strftime and wcsftime functions.

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 non-monetary formatting information returned bylocaleconv.    In addition to the decimal-point character, LC_NUMERIC also sets the thousands separator and the grouping control string returned by localeconv.

//与LC_MONETARY类似,但是需要注意,Decimal-point character (小数点符号)是不需要显示调用的,在printf等函数输出浮点数时,会隐式的使用locale相关的小数点。

比如默认的locale是C,C的小时点就是.。

LC_CTYPE

The character-handling functions (except isdigitisxdigitmbstowcs, and mbtowc, which are unaffected).

不得不吐槽的是msdn关于LC_CTYPE说得不全面+不正确。mbstowcs, and mbtowc,是受LC_CTYPE影响的。

http://www.cplusplus.com/reference/clocale/setlocale/?kw=setlocale的解释貌似是正确的。

Affects character handling functions (all functions of <cctype>, except isdigit and isxdigit), and the multibyte and wide character functions.

具体是哪些方法呢,参考<cctype>我们知道 character handling functions有:

isalnum

 

另外,参考http://gsp.com/cgi-bin/man.cgi?section=3&topic=multibyte和https://msdn.microsoft.com/en-US/library/6y9se58z(v=vs.80).aspx,我们知道 the multibyte and wide character functions.有:

 

Function       Description 
mblen 3      get number of bytes in a character 
mbrlen 3      get number of bytes in a character (restartable) 
mbrtowc 3      convert a character to a wide-character code (restartable) 
mbsrtowcs 3      convert a character string to a wide-character string (restartable) 
mbstowcs 3      convert a character string to a wide-character string 
mbtowc 3      convert a character to a wide-character code 
wcrtomb 3      convert a wide-character code to a character (restartable) 
wcstombs 3      convert a wide-character string to a character string 
wcsrtombs 3      convert a wide-character string to a character string (restartable) 
wctomb 3      convert a wide-character code to a character

转载于:https://www.cnblogs.com/xiaohe9527/p/4655374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以使用 wchar_t 类型来存储 Unicode 编码的字符。如果要将汉字编码转换为 Unicode 编码,可以使用一些库函数,如 mbstowcs() 或 MultiByteToWideChar()。 mbstowcs() 函数可以将多字节字符串转换为宽字符字符串,其语法如下: ``` size_t mbstowcs(wchar_t *dest, const char *src, size_t n); ``` 其中,dest 表示转换后的宽字符字符串,src 表示待转换的多字节字符串,n 表示最多转换的字符数。 示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <locale.h> int main() { // 设置本地化环境 setlocale(LC_ALL, ""); // 待转换的汉字字符串 char *str = "你好,世界!"; // 分配存储宽字符字符串的空间 size_t len = strlen(str); wchar_t *wstr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t)); // 转换为宽字符字符串 size_t ret = mbstowcs(wstr, str, len + 1); // 输出转换结果 wprintf(L"宽字符字符串:%ls\n", wstr); // 释放内存 free(wstr); return 0; } ``` MultiByteToWideChar() 函数是 Windows 系统中的 API 函数,可以将 ANSI 编码或 UTF-8 编码转换为 Unicode 编码。其语法如下: ``` int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar); ``` 其中,CodePage 表示待转换的字符集编码,dwFlags 表示转换选项,lpMultiByteStr 表示待转换的多字节字符串,cbMultiByte 表示待转换的多字节字符串长度(单位为字节),lpWideCharStr 表示转换后的宽字符字符串,cchWideChar 表示存储转换后的宽字符字符串的缓冲区大小(单位为字符)。 示例代码: ```c #include <stdio.h> #include <windows.h> int main() { // 待转换的汉字字符串 char *str = "你好,世界!"; // 分配存储宽字符字符串的空间 int len = strlen(str); int wlen = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0); wchar_t *wstr = (wchar_t *)malloc((wlen + 1) * sizeof(wchar_t)); // 转换为宽字符字符串 MultiByteToWideChar(CP_UTF8, 0, str, len, wstr, wlen + 1); // 输出转换结果 wprintf(L"宽字符字符串:%ls\n", wstr); // 释放内存 free(wstr); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值