Wide chartomultibyte Function

Source: Internet
Author: User
Widechartomultibyte

TheWidechartomultibyteFunction maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.

Int widechartomultibyte (uintCodePage,// Code pageDWORDDwflags,// Performance and mapping flagsLpcwstrLpwidecharstr,// Wide-character stringIntCchwidechar,// Number of chars in string.LpstrLpmultibytestr,// Buffer for New StringIntCbmultibyte,// Size of BufferLpcstrLpdefaultchar,// Default for unmappable charsLpboolLpuseddefaultchar// Set when default char used);
Parameters
CodePage
[In] specifies the code page used to perform the conversion. this parameter can be given the value of any code page that is installed or available in the system. for a list of code pages, see code page identifiers. you can also specify one of the following values.

Value Meaning
Cp_acp ANSI code page
Cp_maccp Macintosh code page
Cp_oemcp OEM code page
Cp_symbol Windows 2000/XP:Symbol code page (42)
Cp_thread_acp Windows 2000/XP:Current thread's ANSI code page
Cp_utf7 Windows 98/Me, Windows NT 4.0 and later:Translate using UTF-7. When this is set,LpdefaultcharAndLpuseddefaultcharMust be null
Cp_utf8 Windows 98/Me, Windows NT 4.0 and later:Translate using UTF-8. When this is set,DwflagsMust be zero and bothLpdefaultcharAndLpuseddefaultcharMust be null.

Windows 95:Under ,WidechartomultibyteAlso supports cp_utf7 and cp_utf8.

Dwflags
[In] specifies the handling of unmapped characters. The function performs more quickly when none of these flags is set. The following flag constants are defined.

Value Meaning
Wc_no_best_fit_chars Windows 98/me and Windows 2000/XP:Any Unicode characters that do not translate directly to multibyte equivalents are translated to the default character (seeLpdefaultcharParameter). In other words, if translating from Unicode to multibyte and back to Unicode again does not yield the exact same UNICODE character, the default character is used.

This flag can be used by itself or in combination with the otherDwflagOptions.

Wc_compositecheck Convert composite characters to precomposed characters.
Wc_discardns Discard nonspacing characters during conversion.
Wc_sepchars Generate separate characters during conversion. This is the default conversion behavior.
Wc_defachar char Replace exceptions with the default character during conversion.

When wc_compositecheck is specified, the function converts composite characters to precomposed characters. A composite Character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/nonspacing character combination. in the character,EIs the base character, and the accent grave mark is the nonspacing character.

When an application specifies wc_compositecheck, it can use the last three flags in this list (wc_discardns, wc_sepchars, and wc_defaultchar) to customize the conversion to precomposed characters. these flags determine the function's behavior when there is no precomposed mapping for a base/nonspace character combination in a wide-character string. these last three flags can only be used if the wc_compositecheck flag is set.

The function's default behavior is to generate separate characters (wc_sepchars) for unmapped composite characters.

For the code pages in the following table,DwflagsMust be zero, otherwise the function fails with error_invalid_flags.

50220

50221

50222

50225

50227

50229

52936

54936

57002 through 57011

65000 (utf7)

65001 (utf8)

42 (Symbol)

Lpwidecharstr
[In] points to the wide-character string to be converted.
Cchwidechar
[In] specifies the number of wide characters in the string pointed to by LpwidecharstrParameter. If this value is-1, the string is assumed to be null-terminated and the length is calculated automatically. The length will include the null-Terminator.

Note that ifCchwidecharIs zero the function fails.

Lpmultibytestr
[Out] points to the buffer to receive the translated string.
Cbmultibyte
[In] specifies the size, in bytes, of the buffer pointed to by LpmultibytestrParameter. If this value is zero, the function returns the number of bytes required for the buffer. (in this case, LpmultibytestrBuffer is not used .)
Lpdefaultchar
[In] points to the character used if a wide character cannot be represented in the specified code page. if this parameter is null, a system default value is used. to obtain the system default character which is used if a wide character cannot be represented in the specified code page, use the getcpinfo or getcpinfoex function. the function is faster when both LpdefaultcharAnd LpuseddefaultcharAre null.

For the code pages mentioned inDwflags,LpdefaultcharMust be null, otherwise the function fails with error_invalid_parameter.

Lpuseddefaultchar
[In] points to a flag that indicates whether a default character was used. the flag is set to true if one or more wide characters in the source string cannot be represented in the specified code page. otherwise, the flag is set to false. this parameter may be null. the function is faster when both LpdefaultcharAnd LpuseddefaultcharAre null.

For the code pages mentioned inDwflags,LpuseddefaultcharMust be null, otherwise the function fails with error_invalid_parameter.

Return values

If the function succeeds, andCbmultibyteIs nonzero, the return value is the number of bytes written to the buffer pointed toLpmultibytestr. The number provided des the byte for the null Terminator.

If the function succeeds, andCbmultibyteIs zero, the return value is the required size, in bytes, for a buffer that can receive the translated string.

If the function fails, the return value is zero. To get extended error information, call .GetlasterrorMay return one of the following error codes:

Error_insufficient_buffer
Error_invalid_flags
Error_invalid_parameter

Remarks Security alert Using Widechartomultibyte Function incorrectly can compromise the security of your application. Calling Widechartomultibyte Function can easily cause a buffer overrun because the size of the in buffer equals the number of wchars in the string, while the size of the out buffer equals the number of bytes. to avoid a buffer overrun, be sure to specify a buffer size appropriate for the data type the buffer has es. for more information, see .

For strings that require validation, such as file, resource and user names, always use the wc_no_best_fit_chars flagWidechartomultibyte. This flag prevents the function from mapping characters to characters that appear similar but have very different semantics. in some cases, the semantic change can be extreme e.g ., symbol for '∞ '(infinity) maps to 8 (eight) in some code pages.

Wc_no_best_fit_chars is not available on Windows 95 and nt4. if your code must run on these platforms, you can achieve the same effect by round tripping the string usingMultibytetowidechar. Any code point that does not round trip is a best-fit character.

TheLpmultibytestrAndLpwidecharstrPointers must not be the same. If they are the same, the function fails, andGetlasterrorReturns error_invalid_parameter.

IfCodePageIs cp_symbol andCbmultibyteIs lessCchwidechar,No characters are writtenLpmultibyte.Otherwise, ifCbmultibyteIs lessCchwidechar,CbmultibyteCharacters are copied to the buffer pointed toLpmultibyte.

An application can useLpdefaultcharParameter to change the default character used for the conversion.

As noted earlier, the widechartomultibyte function operates most efficiently when both lpdefaultchar and lpuseddefaultchar are null. the following table shows the behavior of widechartomultibyte for the four combinations of lpdefaultchar and lpuseddefachar char .

Lpdefaultchar Lpuseddefaultchar Result
Null Null No Default checking. This is the most efficient way to use this function.
Non-null Null Uses the specified default character, but does not setLpuseddefaultchar.
Null Non-null Uses the system default character and setsLpuseddefaultcharIf necessary.
Non-null Non-null Uses the specified default character and setsLpuseddefaultcharIf necessary.

Windows 95/98/me: WidechartomultibyteIs supported by the Microsoft layer for Unicode. To use this, you must add certain files to your application, as outlined in .

Example code

For an example, see looking up a user's full name.

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.