wchar_t在一般编程中有什么用? [英] What is the use of wchar_t in general programming?

查看:232
本文介绍了wchar_t在一般编程中有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我学习了一些C ++基础知识,并且了解了 wchar_t 。我无法弄清楚,为什么我们实际上需要此数据类型,以及如何使用它?

Today I was learning some C++ basics and came to know about wchar_t. I was not able to figure out, why do we actually need this datatype, and how do I use it?

推荐答案

wchar_t 用于以定宽多字节编码表示文本;由于 wchar_t 的大小通常为2个字节,因此可以用来表示任何2字节编码的文本。它也可以用来表示可变宽度多字节编码的文本,其中最常见的是UTF-16。

wchar_t is intended for representing text in fixed-width, multi-byte encodings; since wchar_t is usually 2 bytes in size it can be used to represent text in any 2-byte encoding. It can also be used for representing text in variable-width multi-byte encodings of which the most common is UTF-16.

在平台上其中 wchar_t 的大小为4个字节,可以使用UCS-4(Unicode)表示任何文本,但是由于在大多数平台上只有2个字节,因此只能表示Unicode以可变宽度编码(通常为UTF-16)。更常见的是使用 char 带有可变宽度编码,例如UTF-8或GB 18030。

On platforms where wchar_t is 4 bytes in size it can be used to represent any text using UCS-4 (Unicode), but since on most platforms it's only 2 bytes it can only represent Unicode in a variable-width encoding (usually UTF-16). It's more common to use char with a variable-width encoding e.g. UTF-8 or GB 18030.

关于唯一广泛使用 wchar_t 的现代操作系统是Windows。这是因为Windows在扩展到U + FFFF之前采用了Unicode,因此固定宽度的2字节编码(UCS-2)显得比较明智。现在UCS-2不足以表示整个Unicode,因此Windows使用UTF-16,仍然使用 wchar_t 2字节代码单元。

About the only modern operating system to use wchar_t extensively is Windows; this is because Windows adopted Unicode before it was extended past U+FFFF and so a fixed-width 2-byte encoding (UCS-2) appeared sensible. Now UCS-2 is insufficient to represent the whole of Unicode and so Windows uses UTF-16, still with wchar_t 2-byte code units.

这篇关于wchar_t在一般编程中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆