strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(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) );     

}  

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

时间: 2024-10-12 19:56:16

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

-bash: warning: setlocale: LC_CTYPE: cannot change locale (EN_US.UTF-8)

最近发现MDT推出去的系统的有不同问题,其问题就不说了,主要是策略权限被域继承了.比如我们手动安装的很多东东都是未配置壮态,推的就默认为安全壮态了,今天细找了一下,原来把这个关了就可以了. -bash: warning: setlocale: LC_CTYPE: cannot change locale (EN_US.UTF-8)

warning: setlocale: LC_CTYPE: cannot change locale

setlocale warning 使用mac上面iterm2一段时间了,登陆服务器老是报如下warning warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory 然后yum 安装的时候也会报LC_CTYPE环境变量的问题.于是抽空搜索了一下,找到了如下文章http://www.cyberciti.biz/faq/os-x-terminal-bash-warning-setloc

登陆报错:-bash: warning: setlocale: LC_CTYPE: cannot c

Xshell登录linux系统时会出现报错:-bash: warning: setlocale: LC_CTYPE: cannot change locale (latin1): No such file or directory-bash: warning: setlocale: LC_COLLATE: cannot change locale (latin1): No such file or directory-bash: warning: setlocale: LC_MESSAGES:

MSVCRT.DLL Console I/O Bug(setlocale(LC_CTYPE, &quot;Chinese_China.936&quot;))

I have been quite annoyed by a Windows bug that causes a huge number of open-source command-line tools to choke on multi-byte characters at the Windows Command Prompt. The MSVCRT.DLL shipped with Windows Vista or later has been having big troubles wi

终端SSH远程连接CentOS报错:-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

终端SSH远程连接CentOS时,报以下错误提示: -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory 在centos服务器上 sudo vim /etc/locale.conf 内容如下: LC_ALL=en_US.utf8 LC_CTYPE=en_US.utf8 LANG=en_US.utf8 重新连接就正常了. 原文地址:https://www.cnblog

警告:setlocale: LC_CTYPE: 无法改变区域选项 (UTF-8)

vi /etc/environment 添加下面几行 LANG=en_US.utf-8 LC_ALL=en_US.utf-8 安装 yum install glibc-common yum -y install kde-l10n-Chinese telnet 然后 localectl set-locale LANG=zh_CN.utf8 修改 /etc/locale.conf 设置 LANG=zh_CN.utf8 重启 原文地址:https://blog.51cto.com/quietnight

Mac iTerm2登陆CentOS提示warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

[报错原因]:没有utf-8这个语系(没添加语言_国名前缀),LC_ALL又没设定值. 在 /etc/environment 加入以下两行,重新登陆即可. LANG=en_US.utf-8 LC_ALL=en_US.utf-8 原文地址:https://www.cnblogs.com/imzye/p/8503576.html

【转载】strlen与sizeof区别

自己小结: sizeof使用时,若是数组变量,则是数组变量占的大小 char a[10]; sizeof(a)=10 若是指针,则为指针大小,数组变量作为函数参数传递时,会退化成指针,且函数内是不知道原数组大小的 char *s="abcdddddd"; sizeof(s) = 4 指针 void f(char a[]) {sizeof(a) = 4 指针} 下面转载自百度百科 ------------------------------------------------------

strlen函数的详细说明

(一)函数定义原型: extern unsigned int strlen(char *s); 在Visual C++ 6.0或Dev-C++中,原型为: size_t strlen(const char *string); 其中size_t实际上是unsigned int,在VC6.0或Dev-C++中可以看到这样的代码: typedef unsigned int size_t; 头文件:string.h或cstring 格式:strlen (字符指针表达式) 功能:计算给定字符串的(unsi