フォーマット
#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 (変換) に 設定される可能性があります。
wcstombs() の使用例
このプログラムは 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 */
関連情報