Unicode programming in Windows

Source: Internet
Author: User

When installing Visual Studio in the Unicode environment, you must add the Unicode option when selecting VC ++ to ensure that the relevant library files can be copied to system32. Unicode compilation settings: C/C ++ and Preprocessor difinitions remove _ MBCS and add _ Unicode. Unicode sets entry to wwinmaincrtstartup in projectsetting/link/output and MBCS (ANSI) compiling. UNICODE: Wide-Byte Character Set 1. How to obtain the number of characters in a string that contains both single-byte and double-byte characters? You can call the Runtime Library of Microsoft Visual C ++ to contain the function _ mbslen to operate multi-byte strings (including single-byte and dual-byte strings. Calling the strlen function does not really know how many characters are in the string. It only tells you how many bytes are before the end of 0. 2. How to operate on DBCS strings? Function Description: ptstr charnext (lpctstr); returns the address of the next character in the string ptstr charprev (lpctstr, lpctstr); returns the address bool isdbcsleadbyte (byte) of the previous character in the string ); if this byte is the first byte of the DBCS character, a non-0 value 3 is returned. why use Unicode? (1) It is easy to exchange data between different languages. (2) enable you to allocate a single. EXE file or DLL file that supports all languages. (3) improve the running efficiency of applications. Windows 2000 is developed from scratch using Unicode. If you call any windows function and pass it an ANSI string, the system must first convert the string to Unicode, then, the Unicode string is passed to the operating system. If you want the function to return an ANSI string, the system first converts the Unicode string to an ANSI string and then returns the result to your application. To convert these strings, the system time and memory are required. By developing applications with Unicode from the beginning, you can make your applications run more effectively. Windows CE itself is a unicode operating system. It does not support ANSI Windows functions. Windows 98 only supports ANSI and can only develop applications for ANSI. When Microsoft converts com from a 16-bit windows to Win32, the company determines that all the COM interface methods that require strings can only accept Unicode strings. 4. How to compile Unicode source code? Microsoft has designed windowsapi for Unicode to minimize the impact of code. In fact, you can write a single source code file to compile it with or without Unicode. You only need to define two macros (Unicode and _ Unicode) to modify and re-compile the source file. _ Unicode macro is used for the C Runtime header file, while Unicode macro is used for the Windows header file. When compiling the source code module, these two macros must be defined at the same time. 5. What Unicode data types are defined in windows? Data Type Description: The wchar Unicode Character pwstr points to the Unicode string pointer pcwstr points to a constant Unicode string pointer corresponding to the ANSI data type is Char, lpstr and lpcstr. The Common Data Types of ANSI/Unicode are tchar, ptstr, and lpctstr. 6. How to operate Unicode? Character Set feature instance ANSI operation function strcpyunicode operation function starts with str wcscpymbcs operation function starts with _ MBS _ mbscpyansi/Unicode operation function starts with _ TCS _ tcscpy (C Runtime Library) ANSI/Unicode operation functions start with lstr lstrcpy (Windows function). All new and outdated functions have both ANSI and Unicode versions in Windows2000. Functions of the ANSI version end with a, and functions of the Unicode version end with W. Windows will define the following definitions: # ifdef Unicode # define createwex create‑wexw # else # define create‑wex create‑wexa # endif //! Unicode 7. How to represent a unicode String constant? Character Set instance ANSI "string" Unicode L "string" ANSI/Unicode T ("string") or _ text ("string ") if (szerror [0] ==_text ('J') {} 8. why should I try to use operating system functions? Secret. Because these functions are used a lot, they may have been loaded into RAM when the application is running. Such as strcat, strchr, strcmp, and strcpy. 9. How do I write ANSI and Unicode-compliant applications? (1) treat a text string as a character array instead of a chars array or byte array. (2) Use common data types (such as tchar and ptstr) for text characters and strings. (3) Use explicit data types (such as byte and pbyte) for byte, byte pointer, and data cache. (4) use the text macro for the original characters and strings. (5) perform global replacement (for example, replace pstr with ptstr ). (6) Modifying string operations. For example, a function usually needs to pass a cached size in characters, rather than bytes. This means that sizeof (szbuffer) should not be passed, but sizeof (szbuffer)/sizeof (tchar) should be passed ). In addition, if you need to allocate a memory block to the string and have the number of characters in the string, remember to allocate memory by byte. This means that you should call malloc (ncharacters * sizeof (tchar) instead of malloc (ncharacters ). 10. How to compare the selected strings? It is implemented by calling comparestring. Flag description norm_ignorecase ignore uppercase and lowercase letters norm_ignorekanatype do not distinguish hirakana from katakana character norm_ignorenonspace ignore no-interval character escape ignore character norm_ignorewidth do not distinguish between single-byte character and double-byte character same symbols are processed as common symbols 11. how can I determine whether a text file is ANSI or Unicode? If the first two bytes of a text file are 0xff and 0xfe, Unicode is used; otherwise, ANSI is used. 12. How can I determine whether a string is ANSI or Unicode? Use istextunicode for determination. Istextunicode uses a series of statistical and qualitative methods to guess the cached content. Because this is not an exact scientific method, istextunicode may return incorrect results. 13. How to convert a string between Unicode and ANSI? The Windows function multibytetowidechar is used to convert a multi-byte string to a wide string. The function widechartomultibyte converts a wide string to an equivalent multi-byte string. 14. Differences between Unicode and DBCS Unicode usage (especially in the C programming language environment) "wide character set ". 「 Every character in Unicode is a 16-Bit Width, not an 8-Bit Width .」 In Unicode, there is no meaning to simply use an 8-bit value. In contrast, we still PROCESS 8-bit values in the double-bit character set. Some bit groups Define characters, while some bit groups show that one character needs to be defined together with another bit group. Processing DBCS strings is messy, but processing Unicode text is like processing ordered text. You may be glad to know that the first 128 Unicode characters (16-bit code from 0x0000 to 0x007f) are ASCII characters, and the next 128 Unicode characters (code from 0x0080 to 0x00ff) is an ASCII extension of ISO 8859-1. The characters in different parts of Unicode are also based on the existing standards. This is to facilitate conversion. The Greek alphabet uses code from 0x0370 to 0x03ff, the codes from 0x0400 to 0x04ff are used in the Slavic language, the codes from 0x0530 to 0x058f are used in the United States, and the codes from 0x0590 to 0x05ff are used in the Hebrew language. Hieroglyphics (CJK) in China, Japan, and South Korea occupy code from 0x3000 to 0x9fff. The biggest advantage of Unicode is that there is only one character set and there is no ambiguity. 15. The derived standard Unicode is a standard. UTF-8 is its conceptual subset, and UTF-8 is the specific coding standard. Unicode is the standard for all requests to meet the world's unified encoding standards. The UTF-8 standard is Unicode (iso000046) standard of a deformation mode, UTF full name is: Unicode/UCOS Transformation format, in fact there are two UTF, one is the UTF-8, one is the UTF-16, but the UTF-16 is used less, its correspondence is as follows: in Unicode encoding for 0000-007f UTF-8 the encoding form is: 0 xxxxxxx in Unicode encoding 0080-07ff in the form of UTF-8 encoding: 110 XXXXX 10xxxxxx encoded in Unicode 0000-007f in the form of UTF-8 encoding: 1110 XXXX 10 xxxxxx 10 xxxxxx UTF-8 is a new Unicode encoding standard. In fact, there are several Unicode standards. we know that the Unicode Character inner code used for a long time is 16 bits. In fact, it cannot compile all the characters in the world in a flat system, such Therefore, UTF-8 is extended to 32 characters, which means that UTF-8 can contain up to characters. unicode is designed to encode all characters in a unified way. big5 and GB are independent character sets. This is also called the Far East character set. If you get it to the German version of Windows, it may cause character encoding conflicts .... in earlier windows, the default character set was ANSI. the Chinese characters entered in Notepad are locally encoded, But Unicode can be directly supported within NT/2000. Notepad.exe is an ANSI character in Win95 and 98, and Unicode in NT. ANSI and Unicode can easily implement ing, that is, converting ASCII is an eight-bit character set, which cannot be expressed for characters outside the range, such as Chinese characters. Unicode is a character set within the 16-bit range. For character Partition Distribution in different regions, Unicode is a character encoding standard jointly developed by multiple it giants. In Unicode environments such as Windows NT, a single character occupies 16 bytes, while in ANSI environments such as Windows 98, the next character occupies 8 bytes. the Unicode character is a 16-bit width, which can contain a maximum of 65,535 characters. The data type is called wchar. For existing ANSI characters, Unicode simply expands to 16 bits: for example, ANSI "A" = 0x43, the corresponding Unicode is "A" = 0x0043, and ASCII is stored 128 characters in seven. ASCII is a real American standard, so it cannot meet the needs of other countries, for example, the English letters and Chinese characters in the Slavic language appear in the Windows ANSI character set. It is an extended ASCII code that stores 8 characters and stores the original ASCII code at a low of 128 characters, if def Unicode tchar = wcharelse tchar = char, such as a Greek letter, is added to the 128-bit height, you need to add Unicode and _ Unicode uincode to project/settings/C ++/preprocesser definitions., _ Unicode must be defined. If _ Unicode is not defined, settext (hwnd, lpctstr) will be interpreted as settexta (hwnd, lptstr). In this case, the API regards the Unicode string you give as an ANSI string, garbled characters are displayed. Because Windows APIs are compiled and stored in DLL, both Unicode and ANSI strings are considered as a buffer, for example, "0b A3 00 35 24 3C 00 00" if read by ANSI, because the ANSI string ends with '/0', only two bytes can be read. "0b A3/0 ", if you read data by Unicode, the complete read '/123456' ends. Since Unicode does not have any additional indications, the system must know the format of the string you provide. In addition, Unicode seems to be specified by ansi c ++, and _ Unicode is provided by the Windows SDK. If you do not write a Windows program, you can only define Unicode. Development Process: it focuses on file read/write and string processing. There are two main types of files :. TXT and. INI File 1. in Unicode and non-Unicode environments, strings are processed differently. You need to refer to the preceding two articles (9 and 10) to meet the character string processing requirements in different environments. The same is true for reading and writing files. When you call related interface functions, add _ text and other related macros to the strings in the parameter. If the file to be written must be saved in unicode format, you must add a byte header when creating the file. Cfile file; wchar szwbuffer [128]; wchar * pszunicode = l "Unicode string/N"; // Unicode string char * pszansi = "ANSI string/N "; // ANSI string word wsignature = 0 xfeff; file. open (text ("test.txt"), cfile: modecreate | cfile: modewrite); file. write (& wsignature, 2); file. write (pszunicode, lstrlenw (pszunicode) * sizeof (wchar); // explicitly use lstrlenw function multibytetowidechar (cp_acp, 0, pszansi,-1, Szwbuffer, 128); file. write (szwbuffer, lstrlenw (szwbuffer) * sizeof (wchar); file. close (); // The above code is valid in both Unicode and non-Unicode environments. Here, Unicode is explicitly used for operations. 2. In non-Unicode environments, all strings in ANSI format are called by default. In this case, tchar is converted to char type unless wchar is explicitly defined. Therefore, in this environment, if you want to read a Unicode file, you must first move two bytes. Then, you must use multibytetowidechar to convert the read string. After conversion, the string information indicates Unicode data. 3. In the Unicode environment, all strings in unicode format are called by default, that is, wide characters. At this time, tchar is converted to wchar, and related API functions also call wide character functions. At this time, the Unicode file is read in the same way as the preceding one, but the data obtained by reading is wchar. to convert it to ANSI format, you need to call widechartomultibyte. If you want to read ANSI data, you do not need to move two bytes to directly read the data and convert the data as needed. Some languages (such as Korean) must be displayed in the Unicode environment. In this case, the development in a non-Unicode environment cannot achieve the purpose of displaying text even if string function conversion is used, because at this time, the API function is called using ANSI (although the underlying layer uses Unicode processing, the processing result is displayed according to the API called by the programmer ). Therefore, Unicode must be used for development.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.