wcstombs ()- 将宽字符字符串转换为多字节字符串
格式
#include <stdlib.h>
size_t wcstombs(char *dest, const wchar_t *string, size_t count);
语言级别
ANSI
线程安全
是
语言环境敏感
此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 如果在编译命令中指定了 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF) ,那么此行为也可能受当前语言环境的 LC_UNI_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境。
宽字符函数
有关更多信息,请参阅 宽字符 。
描述
wcstombs()
函数将 string 指向的宽字符字符串转换为 dest指向的多字节数组。 转换后的字符串以初始移位状态开始。 在 dest 中的 count 字节填满或迂到 wchar_t 空字符后,转换停止。
仅完整的多字节字符存储在 dest中。 如果 dest 中缺少空间将导致存储部分多字节字符,那么 wcstombs()
将存储少于 n 字节的字符并废弃无效字符。
返回值
wcstombs()
函数返回多字节字符串的长度 (以字节为单位) ,不包含结束空字符。 如果迂到无效的多字节字符,那么将返回值 (size_t) -1 。
errno
的值可以设置为 EILSEQ (由于输入字符而停止转换) 或 ECONVERT (转换错误)。
示例
此程序使用 LOCALETYPE (*LOCALE) 和 SYSIFCOPT (*IFSIO) 进行编译:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#define STRLENGTH 10
#define LOCNAME "/qsys.lib/JA_JP.locale"
#define LOCNAME_EN "/qsys.lib/EN_US.locale"
int main(void)
{
char string[STRLENGTH];
int length, sl = 0;
wchar_t wc2[] = L"ABC";
wchar_t wc_string[10];
mbstate_t ps = 0;
memset(string, '\0', STRLENGTH);
wc_string[0] = 0x00C1;
wc_string[1] = 0x4171;
wc_string[2] = 0x4172;
wc_string[3] = 0x00C2;
wc_string[4] = 0x0000;
/* In this first example we will convert a wide character string */
/* to a single byte character string. We first set the locale */
/* to a single byte locale. We choose a locale with */
/* CCSID 37. */
if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
printf("setlocale failed.\n");
length = wcstombs(string, wc2, 10);
/* In this case wide characters ABC are converted to */
/* single byte characters ABC, length is 3. */
printf("string = %s, length = %d\n\n", string, length);
/* Now lets try a multibyte example. We first must set the */
/* locale to a multibyte locale. We choose a locale with */
/* CCSID 5026 */
if (setlocale(LC_ALL, LOCNAME) == NULL)
printf("setlocale failed.\n");
length = wcstombs(string, wc_string, 10);
/* The hex look at string would now be: */
/* C10E417141720FC2 length will be 8 */
/* You would need a device capable of displaying multibyte */
/* characters to see this string. */
printf("length = %d\n\n", length);
}
/* The output should look like this:
string = ABC, length = 3
length = 8
*/
此程序是使用 LOCALETYPE (*LOCALEUCS2) 和 SYSIFCOPT (*IFSIO) 编译的:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#define STRLENGTH 10
#define LOCNAME "/qsys.lib/JA_JP.locale"
#define LOCNAME_EN "/qsys.lib/EN_US.locale"
int main(void)
{
char string[STRLENGTH];
int length, sl = 0;
wchar_t wc2[] = L"ABC";
wchar_t wc_string[10];
mbstate_t ps = 0;
memset(string, '\0', STRLENGTH);
wc_string[0] = 0x0041; /* UNICODE A */
wc_string[1] = 0xFF41;
wc_string[2] = 0xFF42;
wc_string[3] = 0x0042; /* UNICODE B */
wc_string[4] = 0x0000;
/* In this first example we will convert a wide character string */
/* to a single byte character string. We first set the locale */
/* to a single byte locale. We choose a locale with */
/* CCSID 37. */
if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
printf("setlocale failed.\n");
length = wcstombs(string, wc2, 10);
/* In this case wide characters ABC are converted to */
/* single byte characters ABC, length is 3. */
printf("string = %s, length = %d\n\n", string, length);
/* Now lets try a multibyte example. We first must set the */
/* locale to a multibyte locale. We choose a locale with */
/* CCSID 5026 */
if (setlocale(LC_ALL, LOCNAME) == NULL)
printf("setlocale failed.\n");
length = wcstombs(string, wc_string, 10);
/* The hex look at string would now be: */
/* C10E428142820FC2 length will be 8 */
/* You would need a device capable of displaying multibyte */
/* characters to see this string. */
printf("length = %d\n\n", length);
}
/* The output should look like this:
string = ABC, length = 3
length = 8
*/