c语言宽字符串头文件,C语言中的宽字符串的使用

copy:http://luckybirdtom.blog.hexun.com/40835185_d.html

通过设置区域可以格式化字符、日期和为货币显示格式为本地。C语言支持这些特性使得C语言可以直接支持中文。C语言中的宽字符与多字节字符的简介可以参考后面的介绍。

unicode宽字符串使用方法如下:包含头文件#include,程序初始处写语句setlocale(LC_ALL, "Chinese"); 声明变量时应该在字符串前加上L wchar_t ws[100] = L"俄罗斯russia"; 一般的字符串处理函数都换成以wcs开头的版本, printf("%d\n", wcslen(ws) ); 则会输出9(“俄罗斯”算三个字符)。如果printf的格式串使用的是宽字符串,则应该使用wprintf。相关的其他一些函数还有有wctomb,wcstombs ,mbstowcs,mbtowc在两种字符串之间转换。

这样就可以直接处理中文字符串了,不会出现将一个中文汉字当成两个字母处理所出现的种种问题。

下面是一段示例代码(在vc60环境下使用)

#include

#include

#include

#include

#include

int main()

{

setlocale(LC_ALL, "Chinese"); //设置为中文本地化

wchar_t s[100]=L"俄罗斯123"; //初始化时在字符串前加L

char a[100]="中国China";

mbstowcs(s,a,100); //多字符字符串转为宽字符串

printf("%d\n", sizeof(wchar_t));//每个字符占2字节

printf("%d\n", strlen(a));//str开头是多字节字符串函数 计算长度时一个汉字算2

printf("%d\n", wcslen(s));//wcs开头是宽字符串函数 一个汉字长度算1

printf("%S\n", s);//注意使用大写的S

//下面是一段帮助文档中的示例代码 在中文模式下显示当地的时间

time_t ltime;

struct tm *thetime;

unsigned char str[100];

time (&ltime);

thetime = gmtime(&ltime);

/* %#x is the long date representation, appropriate to

* the current locale */

//setlocale(LC_ALL, "Chinese");

if (!strftime((char *)str, 100, "%#x",(const struct tm *)thetime))

printf("strftime failed!\n");

else

printf("In Chinese locale, strftime returns '%s'\n", str);

/* Set the locale back to the default environment */

setlocale(LC_ALL, "C");

time (&ltime);

thetime = gmtime(&ltime);

if (!strftime((char *)str, 100, "%#x", (const struct tm *)thetime))

printf("strftime failed!\n");

else

printf("In 'C' locale, strftime returns '%s'\n", str);

return 0;

}

wide character (http://en.wikipedia.org/wiki/Wide_character)

Wide character is a computer programming term. It is a vague term used to represent a datatype that is richer than the traditional (8-bit) characters. It is not the same thing as Unicode.

wchar_t is a data type in ANSI/ISO C and some other programming languages that is intended to represent wide characters.

The Unicode standard 4.0 says that

"ANSI/ISO C leaves the semantics of the wide character set to the specific implementation but requires that the characters from the portable C execution set correspond to their wide character equivalents by zero extension."

and that

"The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers."

Under Win32, wchar_t is 16 bits wide and represents a UTF-16 code unit. On Unix-like systems wchar_t is commonly 32 bits wide and represents a UTF-32 code unit.

In ANSI C library header files, and deal with the wide characters.

什么是C语言中的宽字符与多字节字符

来源:www.dzsc.com/data/html/2008-9-12/69107.html

C语言原本是在英文环境中设计的,主要的字符集是7位的ASCII码,8位的byte(字节)是最常见的字符编码单位。但是国际化软件必须能够表示不同的字符,而这些字符数量庞大,无法使用一个字节编码。

C95标准化了两种表示大型字符集的方法:宽字符(wide character,该字符集内每个字符使用相同的位长)以及多字节字符(multibyte character,每个字符可以是一到多个字节不等,而某个字节序列的字符值由字符串或流(stream)所在的环境背景决定)。

自从1994年的增补之后,C语言不只提供char类型,还提供wchar_t类型(宽字符),此类型定义在stddef.h 头文件中。wchar_t指定的宽字节类型足以表示某个实现版本扩展字符集的任何元素。

在多字节字符集中,每个字符的编码宽度都不等,可以是一个字节,也可以是多个字节。源代码字符集和运行字符集都可能包含多字节字符。多字节字符可以被用于字符的常量、字符串字面值(string literal)、标识符(identifier)、注释(comment),以及头文件。

C语言本身并没有定义或指定任何编码集合,或任何字符集(基本源代码字符集和基本运行字符集除外),而是由其实现指定如何编码宽字符,以及要支持什么类型的多字节字符编码机制。

虽然C标准没有支持Unicode字符集,但是许多实现版本使用Unicode转换格式UTF-16和UTF-32来处理宽字符。如果遵循Unicode标准,wchar_t类型至少是16或32位长,而wchar_t类型的一个值就代表一个Unicode字符。

UTF-8是一个由Unicode Consortium(万国码联盟)定义的实现,可以表示Unicode字符集的所有字符。UTF-8字符所使用的空间大小从一个字节到四个字节都有可能。

多字节字符和宽字符(也就是wchar_t)的主要差异在于宽字符占用的字节数目都一样,而多字节字符的字节数目不等,这样的表示方式使得多字节字符串比宽字符串更难处理。比方说,即使字符'A'可以用一个字节来表示,但是要在多字节的字符串中找到此字符,就不能使用简单的字节比对,因为即使在某个位置找到相符合的字节,此字节也不见得是一个字符,它可能是另一个不同字符的一部分。然而,多字节字符相当适合用来将文字存储成文件。

C提供了一些标准函数,可以将多字节字符转换为wchar_t,或将宽字符转换为多字节字符。比方说,如果C 编译器使用Unicode 标准的UTF-16 和UTF-8,那么下面调用wctomb()函数就可以获得字符的多字节表示方式(注:wctomb = wide character to multibyte)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值