機能 |
文字列を現地域に適切な文字列に変換する。 ※ 地域(LC_COLLATE部門)によって結果は異なる |
||||||
---|---|---|---|---|---|---|---|
定義 |
#include <string.h> size_t strxfrm ( char * s1, const char * s2, size_t n ); ※C99規格では以下の定義となる size_t strxfrm ( char * restrict s1, const char * restrict s2, size_t n ); |
||||||
引数 |
|
||||||
戻り値 | 変換後の文字列の長さを返す。 | ||||||
実装例 |
#include <string.h> #include <stdio.h> #include <locale.h> void binaryPrint(const char * s, int len); int main(void) { char str[10]; size_t ret; /* 地域をスペイン語に設定 */ printf("locale=%s\n", setlocale(LC_COLLATE, "Spanish")); /* 通常はアルファベット順のためciの方が大きい */ printf("== normal ==\n"); printf("ch="); binaryPrint("ch", 2); printf("ci="); binaryPrint("ci", 2); /* アルファベット順で比較 */ printf("strcmp( ch, ci ) = %d\n", strcmp("ch", "ci")); /* スペイン語の地域ではchの方が大きくなる */ printf("== spanish ==\n"); ret = strxfrm(str, "ch", sizeof(str)); printf("ch="); binaryPrint(str, ret); ret = strxfrm(str, "ci", sizeof(str)); printf("ci="); binaryPrint(str, ret); /* スペイン語の地域における比較 */ printf("strcoll( ch, ci ) = %d\n", strcoll("ch", "ci")); return 0; } /* 文字列をバイナリ表示する関数 */ void binaryPrint(const char * s, int len) { int i; for (i=0; i<len; i++) { printf("%02x ", ((int)(s[i]) & 0xff)); } printf("\n"); } |
||||||
実行結果 |
locale=Spanish_Spain.1252 == normal == ch=63 68 ci=63 69 strcmp( ch, ci ) = -1 == spanish == ch=0e 0d 01 01 01 01 ci=0e 0a 0e 32 01 01 01 01 strcoll( ch, ci ) = 1 |
strxfrm()