使Win32应用程序在ANSI& UNICODE [英] Making Win32 Application work on ANSI & UNICODE

查看:185
本文介绍了使Win32应用程序在ANSI& UNICODE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使我的Win32 C ++应用程序能够在任何编码版本(UNICODE& ANSI)上播放。现在我有点混淆了两个(或更多?)编码之间的区别究竟是什么?



使我的Win32应用程序交叉编码兼容这是否意味着我必须通过我的代码&用std :: wstring替换每个std :: string,然后用wchar_t *替换每个字符,然后用L替换每个字符串()?



如果我的应用程序在UNICODE机器上运行,会发生什么?我的应用程序有一个std :: string吗?



您是否有任何建议,我需要采取的步骤,使我的应用程序交叉编码兼容?
例如:
- 更改所有c_strings&字符串到其UNICODE等价物
- 将任何Win32函数更改为uncide版本(例如,从getenv()更改为_wgetenv())

解决方案

如果我的应用程序运行在UNICODE机器上,会发生什么我的应用程序有一个std :: string?



计算机不是 ANSI Unicode ,但计算机所在的操作系统是。不支持Unicode的最后一个版本的Windows是Windows 3.11 for Workgroups。如果您在UniCode上运行ASCII编译的应用程序。



两个(或多个)编码之间的区别究竟是什么? >



什么是ASCII?

ASCII是一个七在美国英语中最常使用的128个字符中的每一个分配一个数字的位编码技术。这允许大多数计算机记录和显示基本文本。 ASCII不包括在其他国家/地区经常使用的符号。



什么是Unicode?

一个主要的回溯到ASCII,你只能有256个不同的字符。然而,日语和阿拉伯语的语言有数千个字符。因此ASCII在这些情况下不起作用。结果是Unicode,允许多达65,536个不同的字符。



Unicode是ISO和Unicode Consortium开发一种包含每个已写入字母的电子文本编码系统的尝试。 Unicode使用8位,16位或32位字符,具体取决于具体的表示形式,因此Unicode文档通常要求ASCII或Latin-1文件的磁盘空间高达两倍。 Unicode的前256个字符与Latin-1相同。



在Win32中,UNICODE支持#define-ing UNICODE _UNICODE 宏。这反过来会导致您的程序使用Win32功能的Unicode变体。



您是否对我需要的步骤有任何建议让我的应用程序交叉编码兼容?



每个Win32函数(需要或返回一个字符串)有两个变体,一个用于ASCII,一个用于Unicode。并且根据是否定义了UNICODE宏,函数调用解析为其中之一。所以你应该定义宏并开始使用Unicode版本的函数。例如:



std :: string 替换为 std :: wstring

char 替换为 wchar_t *

string()替换为 L

使用的 TCHAR 支持在Windows等。



正如您所指出的,您将拥有的一系列事项照顾,记住你这不是完整的清单。



基本上,您必须在代码中使用所有Unicode版本的类型和函数调用。


I want to make my Win32 C++ application able to be played on any encoding version (UNICODE & ANSI). Now I am a little confused as to what exactly is the difference between the two(or more?) encodings?

To make my Win32 application cross-encoding compatible does that mean I have to go through my code & replace every std::string with std::wstring, then replace every char with a wchar_t* and then replace every literal string("") with L""?

What will happen if my application runs on a UNICODE machine & my application has one std::string in it?

Do you have any advice on the steps I need to take to make my application cross-encoding compatible? For eg: - Change all c_strings & strings to their UNICODE equivalent - Change any Win32 functions to the uncide version (eg, change from getenv() to _wgetenv())

解决方案

What will happen if my application runs on a UNICODE machine & my application has one std::string in it?

Computers are not ANSI or Unicode but the Operating Systems on which the computers operate on are. The last version of Windows that didn't support Unicode was Windows 3.11 for Workgroups. If you run a ASCII compiled application on a UniCode.

What exactly is the difference between the two(or more?) encodings?

What is ASCII?
ASCII is a seven-bit encoding technique which assigns a number to each of the 128 characters used most frequently in American English. This allows most computers to record and display basic text. ASCII does not include symbols frequently used in other countries.

What is Unicode?
One major draw back to ASCII was you could only have 256 different characters. However, languages such as Japanese and Arabic have thousands of characters. Thus ASCII would not work in these situations. The result was Unicode which allowed for up to 65,536 different characters.

Unicode is an attempt by ISO and the Unicode Consortium to develop a coding system for electronic text that includes every written alphabet in existence. Unicode uses 8-, 16-, or 32-bit characters depending on the specific representation, so Unicode documents often require up to twice as much disk space as ASCII or Latin-1 documents. The first 256 characters of Unicode are identical to Latin-1.

In Win32, UNICODE is supported by #define-ing the UNICODE and _UNICODE macros. This, in turn, causes your program to use the Unicode variants of the Win32 functions.

Do you have any advice on the steps I need to take to make my application cross-encoding compatible?

Each Win32 function (that takes or returns a string) has two variants, one for ASCII and one for Unicode. And the function call resolves to one of these, depending on whether or not the UNICODE macro is defined. So you should define the macro and start using the Unicode versions of the functions. for eg:

Replacing every std::string with std::wstring,
Replacing every char with a wchar_t*
Replacing every literal string("") with L""
Making use of the TCHAR support in Windows etc.

as you pointed out are a list of things that you will have to take care of, mind you this is not the complete list.

Basically, You will have to use all the Unicode versions of the types and function calls in your code.

这篇关于使Win32应用程序在ANSI& UNICODE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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