mbstowcs ()- 将多字节字符串转换为宽字符字符串

格式

#include <stdlib.h>
size_t mbstowcs(wchar_t *pwc, const char *string, size_t n);
 

语言级别

ANSI

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 类别影响。 如果在编译命令上指定了 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF) ,那么此函数也可能受当前语言环境的 LC_UNI_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

宽字符函数

有关更多信息,请参阅 宽字符

描述

mbstowcs() 函数确定 string所指向的多字节字符序列的长度。 然后,它将以初始移位状态开始的多字节字符串转换为宽字符串,并将宽字符存储到 pwc指向的缓冲区中。 最多可写入 n 个宽字符。

返回值

mbstowcs() 函数返回生成的宽字符数,不包括任何结束的空宽字符。 如果迂到无效的多字节字符,那么该函数将返回 (size_t) -1

如果发生转换错误,那么 errno 可能设置为 ECONVERT

示例

/* This program is compiled with LOCALETYPE(*LOCALEUCS2) and   */
/* SYSIFCOPT(*IFSIO)                                           */

#include   <stdio.h>
#include   <stdlib.h>
#include   <locale.h>
#include   <wchar.h>
#include   <errno.h>

#define  LOCNAME     "/qsys.lib/JA_JP.locale"
#define  LOCNAME_EN  "/qsys.lib/EN_US.locale"

int main(void)
{
    int length, sl = 0;
    char  string[10];
    char  string2[] = "ABC";
    wchar_t buffer[10];
    memset(string, '\0', 10);
    string[0] = 0xC1;
    string[1] = 0x0E;
    string[2] = 0x41;
    string[3] = 0x71;
    string[4] = 0x41;
    string[5] = 0x72;
    string[6] = 0x0F;
    string[7] = 0xC2;
    /* In this first example we will convert                  */
    /* a multibyte character when the CCSID of locale         */
    /* associated with LC_CTYPE is 37.                        */

    if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
        printf("setlocale failed.\n");


    length = mbstowcs(buffer, string2, 10);

    /* In this case length ABC is converted to UNICODE ABC    */
    /* or 0x004100420043.  Length will be 3.                  */

    printf("length = %d\n\n", 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 = mbstowcs(buffer, string, 10);

    /* The buffer now has the value:                           */
    /* 0x004103A103A30042        length is 4                   */

    printf("length = %d\n\n", length);

}
/*  The output should look like this:

length = 3

length = 4
                                   */                            
/* This program is compiled with LOCALETYPE(*LOCALE) and       */
/* SYSIFCOPT(*IFSIO)                                           */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#include <errno.h>

#define  LOCNAME     "/qsys.lib/JA_JP.locale"
#define  LOCNAME_EN  "/qsys.lib/EN_US.locale"

int main(void)
{
    int length, sl = 0;
    char  string[10];
    char  string2[] = "ABC";
    wchar_t buffer[10];
    memset(string, '\0', 10);
    string[0] = 0xC1;
    string[1] = 0x0E;
    string[2] = 0x41;
    string[3] = 0x71;
    string[4] = 0x41;
    string[5] = 0x72;
    string[6] = 0x0F;
    string[7] = 0xC2;
    /* In this first example we will convert                  */
    /* a multibyte character when the CCSID of locale         */
    /* associated with LC_CTYPE is 37.                        */

    if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
        printf("setlocale failed.\n");

    length = mbstowcs(buffer, string2, 10);

    /* In this case length ABC is converted to             */
    /* 0x00C100C200C3.  Length will be 3.                  */

    printf("length = %d\n\n", 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 = mbstowcs(buffer, string, 10);

    /* The buffer now has the value:                           */
    /* 0x00C14171417200C2        length is 4                   */

    printf("length = %d\n\n", length);

}

/*  The output should look like this:

length = 3

length = 4
                                   */                            

相关信息