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
*/