728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 출처 : MSDN | |
// crt_strlen.c | |
// Determine the length of a string. For the multi-byte character | |
// example to work correctly, the Japanese language support for | |
// non-Unicode programs must be enabled by the operating system. | |
#include <string.h> | |
#include <locale.h> | |
int main() | |
{ | |
char* str1 = "Count."; | |
wchar_t* wstr1 = L"Count."; | |
char * mbstr1; | |
char * locale_string; | |
// strlen gives the length of single-byte character string | |
printf("Length of '%s' : %d\n", str1, strlen(str1) ); | |
// wstrlen gives the length of a wide character string | |
wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) ); | |
// A multibyte string: [A] [B] [C] [katakana A] [D] [\0] | |
// in Code Page 932. For this example to work correctly, | |
// the Japanese language support must be enabled by the | |
// operating system. | |
mbstr1 = "ABC" "\x83\x40" "D"; | |
locale_string = setlocale(LC_CTYPE, "Japanese_Japan"); | |
if (locale_string == NULL) | |
{ | |
printf("Japanese locale not enabled. Exiting.\n"); | |
exit(1); | |
} | |
else | |
{ | |
printf("Locale set to %s\n", locale_string); | |
} | |
// _mbslen will recognize the Japanese multibyte character if the | |
// current locale used by the operating system is Japanese | |
printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) ); | |
// _mbstrlen will recognize the Japanese multibyte character | |
// since the CRT locale is set to Japanese even if the OS locale | |
// isnot. | |
printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) ); | |
printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) ); | |
} | |
//-------------------------------------------------------------- | |
// Output | |
// | |
// Length of 'Count.' : 6 | |
// Length of 'Count.' : 6 | |
// Length of 'ABCァD' : 5 | |
// Length of 'ABCァD' : 5 | |
// Bytes in 'ABCァD' : 6 | |
// | |
// 로케일을 적용하여 문자열 길이를 얻는 API 입니다. 지원은 Windows 2000/XP/2003/Vista 에서만 됩니다. | |
// (유닉스/리눅스는 일부만 표준 호환) | |
// 예제에서는 일본 로케일을 적용 하였습니다. | |
// 결과 중 세번째를 보시면(_mbslen) 길이가 5글자로 반환되는 것을 알 수 있습니다. | |
728x90
반응형
'C C++' 카테고리의 다른 글
C++ 구조체 멤버의 오프셋 얻기 (0) | 2007.10.12 |
---|---|
Visual C++ 2005 REDIST (x86) (0) | 2007.10.11 |
32비트 윈도우즈 자료형과 크기 (2) | 2007.09.04 |
Windows API IsWindowUnicode 함수로 유니코드 지원 여부 확인하기 (0) | 2007.08.28 |
Windows API에서 2의 거듭제곱 여부를 확인 (0) | 2007.08.13 |