// 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();
}
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) ); }

https://msdn.microsoft.com/en-us/library/78zh94ax.aspx

strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(MSDN的官方示例)的更多相关文章

  1. 字符串处理函数(strlen wcslen...)

    1.从一个字符串中寻找某个字符最后出现的位置: wcsrchr 2. ANSI      UNICODE     自动 strlen    wcslen      _tcslen strcpy     ...

  2. MacOs -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    1解决iterm远程登录主机报错 -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or ...

  3. -bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_US.UTF-8): No such file or directory -bash: warning: setlocale: LC_COLLATE:

    前几天登录服务器发现出现了这些个警告,一直没时间去处理他,今天难得有空,处理一下并记录下来,希望可以帮助到有需要的朋友. 警告信息如下: Last :: from 10.0.0.1 -bash: wa ...

  4. -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    本人使用mac系统,命令行工具使用的iterm2,登录自己的云主机的时候 每次都要提示如下错误 -bash: warning: setlocale: LC_CTYPE: cannot change l ...

  5. 控制台程序的中文输出乱码问题(export LC_CTYPE=zh_CN.GBK,或者修改/etc/sysconfig/i18n为zh_CN.GBK。使用setlocale(LC_CTYPE, "");会使用默认办法。编译器会将源码做转换成Unicode格式,或者指定gcc的输入文件的编码参数-finput-charset=GBK。Linux下应该用wprintf(L"%ls/n",wstr))

    今天发现用securecrt登陆时,gcc编译出错时会出现乱码,但直接在主机的窗口界面下用Shell编译却没有乱码.查看了一下当时的错误描述,发现它的引号是中文引号,导致在SecureCRT中显示出错 ...

  6. MSVCRT.DLL Console I/O Bug(setlocale(LC_CTYPE, "Chinese_China.936"))

    I have been quite annoyed by a Windows bug that causes a huge number of open-source command-line too ...

  7. -bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_CN.UTF-8mb4): No such file or directory

    前几天登录服务器发现出现了这些个警告,一直没时间去处理他,今天难得有空,处理一下并记录下来,希望可以帮助到有需要的朋友. 警告信息如下: Last login: Tue May :: from 192 ...

  8. -bash: warning: setlocale: LC_CTYPE: cannot change locale (EN_US.UTF-8): No such file or directory

    出问题原因: 通过 su - 切换用户时候,LANG设置为en或者设置错误,切换角色会出现这个错误 如何解决? echo $LANG # 查看 LANG 参数 sed 's#LANG=en#LANG= ...

  9. Centos warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    vi /etc/environment add these lines... LANG=en_US.utf-8 LC_ALL=en_US.utf-8 QQ技术交流群:576269252 ------- ...

随机推荐

  1. 百度地图坐标之间的距离php

    function GetDistance($lat1, $lng1, $lat2, $lng2){ define('PI',3.1415926535898); define('EARTH_RADIUS ...

  2. Android studio在Refresh gradle project卡死,附解决办法

    首先打开android studio项目 找到项目目录gradle\wrapper\gradle-wrapper.properties这个文件 你会看到 #Wed Apr 10 15:27:10 PD ...

  3. [NPM] Run a set of similar npm scripts with a wildcard

    In this lesson we will run a set of scripts that are grouped together with a wildcard using the npm- ...

  4. HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次)

    HTML中DOM对象的属性和方法的层级关系是怎样的?(目录即层次) 一.总结 一句话总结:目录就是测试题 1.document取得元素(get element)的方式有哪几种? 解答:四种,分别是id ...

  5. 检索01-c#中基本数据类型和引用类型的区别

    1.基本定义 基本数据类型包括:整型.浮点型.字符型.结构体.布尔型.日期时间.枚举类型等 引用类型包括:字符串.类.数组.接口等 堆定义:是一种特殊的树形数据结构,每个结点都有一个值,一般由程序员分 ...

  6. Gini 系数与熵的关系

    首先来看二者的基本定义: ⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪H(X)=−∑k=1KpklnpkGini(X)=∑k=1Kpk(1−pk) 将 f(x)=−lnx 在 x=1 处进行一阶泰勒展开(忽略高阶无穷小 ...

  7. 检索05 --static静态方法 和 非静态方法

    C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问一个类的所有实例的同一C#静 ...

  8. 【codeforces 787B】Not Afraid

    [题目链接]:http://codeforces.com/contest/787/problem/B [题意] -水题..题目太吓人 [题解] 只要你在一组里面找到两个数字,它们的绝对值相同,但是正负 ...

  9. 深入Lazy<T>——.NET Framework 4.0

    .NET Framework 4 在一次次跳票中终于发布了,在一次偶然的机会,我看到了 Anytao 的 [你必须知道的.NET]第三十三回,深入.NET 4.0之,Lazy<T>点滴 . ...

  10. Live Unit Testing

    Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是 ...