📄 tmbconv.tex
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Name: tmbconv.tex%% Purpose: Overview of the wxMBConv classes in wxWidgets%% Author: Ove Kaaven%% Modified by:%% Created: 25.03.00%% RCS-ID: $Id: tmbconv.tex,v 1.11 2005/04/08 14:33:36 MW Exp $%% Copyright: (c) 2000 Ove Kaaven%% Licence: wxWindows licence%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{wxMBConv classes overview}\label{mbconvclasses}Classes: \helpref{wxMBConv}{wxmbconv}, wxMBConvLibc, \helpref{wxMBConvUTF7}{wxmbconvutf7}, \helpref{wxMBConvUTF8}{wxmbconvutf8}, \helpref{wxCSConv}{wxcsconv}, \helpref{wxMBConvUTF16}{wxmbconvutf16}, \helpref{wxMBConvUTF32}{wxmbconvutf32}The wxMBConv classes in wxWidgets enable an Unicode-aware application toeasily convert between Unicode and the variety of 8-bit encoding systems stillin use.\subsection{Background: The need for conversion}\label{needforconversion}As programs are becoming more and more globalized, and users exchange documentsacross country boundaries as never before, applications increasingly need totake into account all the different character sets in use around the world. Itis no longer enough to just depend on the default byte-sized character set thatcomputers have traditionally used.A few years ago, a solution was proposed: the Unicode standard. Able to containthe complete set of characters in use in one unified global coding system,it would resolve the character set problems once and for all.But it hasn't happened yet, and the migration towards Unicode has created newchallenges, resulting in "compatibility encodings" such as UTF-8. A largenumber of systems out there still depends on the old 8-bit encodings, hamperedby the huge amounts of legacy code still widely deployed. Even sendingUnicode data from one Unicode-aware system to another may need encoding to an8-bit multibyte encoding (UTF-7 or UTF-8 is typically used for this purpose), topass unhindered through any traditional transport channels.\subsection{Background: The wxString class}\label{conversionandwxstring}If you have compiled wxWidgets in Unicode mode, the wxChar type will becomeidentical to wchar\_t rather than char, and a wxString stores wxChars. Hence,all wxString manipulation in your application will then operate on Unicodestrings, and almost as easily as working with ordinary char strings (youjust need to remember to use the wxT() macro to encapsulate any stringliterals).But often, your environment doesn't want Unicode strings. You could be sendingdata over a network, or processing a text file for some other application. Youneed a way to quickly convert your easily-handled Unicode data to and from atraditional 8-bit encoding. And this is what the wxMBConv classes do.\subsection{wxMBConv classes}\label{wxmbconvclasses}The base class for all these conversions is the wxMBConv class (which itselfimplements standard libc locale conversion). Derived classes includewxMBConvLibc, several different wxMBConvUTFxxx classes, and wxCSConv, whichimplement different kinds of conversions. You can also derive your own classfor your own custom encoding and use it, should you need it. All you need to dois override the MB2WC and WC2MB methods.\subsection{wxMBConv objects}\label{wxmbconvobjects}Several of the wxWidgets-provided wxMBConv classes have predefined instances(wxConvLibc, wxConvFileName, wxConvUTF7, wxConvUTF8, wxConvLocal). You can usethese predefined objects directly, or you can instantiate your own objects.A variable, wxConvCurrent, points to the conversion object that the userinterface is supposed to use, in the case that the user interface is notUnicode-based (like with GTK+ 1.2). By default, it points to wxConvLibc orwxConvLocal, depending on which works best on the current platform.\subsection{wxCSConv}\label{wxcsconvclass}The wxCSConv class is special because when it is instantiated, you can tell itwhich character set it should use, which makes it meaningful to keep manyinstances of them around, each with a different character set (or you cancreate a wxCSConv instance on the fly).The predefined wxCSConv instance, wxConvLocal, is preset to use thedefault user character set, but you should rarely need to use it directly,it is better to go through wxConvCurrent.\subsection{Converting strings}\label{convertingstrings}Once you have chosen which object you want to use to convert your text,here is how you would use them with wxString. These examples all assumethat you are using a Unicode build of wxWidgets, although they will stillcompile in a non-Unicode build (they just won't convert anything).Example 1: Constructing a wxString from input in current encoding.\begin{verbatim}wxString str(input_data, *wxConvCurrent);\end{verbatim}Example 2: Input in UTF-8 encoding.\begin{verbatim}wxString str(input_data, wxConvUTF8);\end{verbatim}Example 3: Input in KOI8-R. Construction of wxCSConv instance on the fly.\begin{verbatim}wxString str(input_data, wxCSConv(wxT("koi8-r")));\end{verbatim}Example 4: Printing a wxString to stdout in UTF-8 encoding.\begin{verbatim}puts(str.mb_str(wxConvUTF8));\end{verbatim}Example 5: Printing a wxString to stdout in custom encoding.Using preconstructed wxCSConv instance.\begin{verbatim}wxCSConv cust(user_encoding);printf("Data: %s\n", (const char*) str.mb_str(cust));\end{verbatim}Note: Since mb\_str() returns a temporary wxCharBuffer to hold the resultof the conversion, you need to explicitly cast it to const char* if you useit in a vararg context (like with printf).\subsection{Converting buffers}\label{convertingbuffers}If you have specialized needs, or just don't want to use wxString, youcan also use the conversion methods of the conversion objects directly.This can even be useful if you need to do conversion in a non-Unicodebuild of wxWidgets; converting a string from UTF-8 to the currentencoding should be possible by doing this:\begin{verbatim}wxString str(wxConvUTF8.cMB2WC(input_data), *wxConvCurrent);\end{verbatim}Here, cMB2WC of the UTF8 object returns a wxWCharBuffer containing a Unicodestring. The wxString constructor then converts it back to an 8-bit characterset using the passed conversion object, *wxConvCurrent. (In a Unicode buildof wxWidgets, the constructor ignores the passed conversion object andretains the Unicode data.)This could also be done by first making a wxString of the original data:\begin{verbatim}wxString input_str(input_data);wxString str(input_str.wc_str(wxConvUTF8), *wxConvCurrent);\end{verbatim}To print a wxChar buffer to a non-Unicode stdout:\begin{verbatim}printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));\end{verbatim}If you need to do more complex processing on the converted data, youmay want to store the temporary buffer in a local variable:\begin{verbatim}const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(unicode_data);const char *tmp_str = (const char*) tmp_buf;printf("Data: %s\n", tmp_str);process_data(tmp_str);\end{verbatim}If a conversion had taken place in cWX2MB (i.e. in a Unicode build),the buffer will be deallocated as soon as tmp\_buf goes out of scope.(The macro wxWX2MBbuf reflects the correct return value of cWX2MB(either char* or wxCharBuffer), except for the const.)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -