HOWTO: Pass a Double-Byte Character to a Win32 API Function (244046)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API)
  • Microsoft Windows XP Home Edition
  • Microsoft Windows XP Professional

This article was previously published under Q244046

SUMMARY

Double-Byte Character Set (DBCS or MBCS) systems use character pairs called lead bytes and trail bytes to represent more than 256 characters. It is sometimes necessary to process a character "pair" in a function call that is designed to accept a single character.

MORE INFORMATION

Some functions in the Win32 API are character-oriented. The interface of these functions accepts one- or two-character code parameters through the UINT data type. However, a DBCS character is defined by two bytes, a lead byte and trail byte, which form a 16-bit character code. The combination of a lead byte and a trail byte to form a 16-bit character code is called a character pair.

Character pairs can be passed to character-oriented functions by packing the lead and trail bytes into the UINT parameter of the function. To pack a DBCS character code into the UINT parameter, place the trail byte in the low-order byte of the UINT and place the lead byte into the next highest order byte of the UINT.

The following PackDBCS sample function demonstrates this technique:
UINT    PackDBCS(BYTE Lead, BYTE Trail)
{
    UINT nChar = 0;

    nChar = Lead;           // Lead byte first.
    nChar = nChar << 8;     // Shift to next highest order byte.
    nChar = nChar | Trail;  // Then trail byte.

    return nChar;
}
				
The PackDBCS function can be used in situations similar to that shown in the following code sample, where the pStr variable is assumed to be properly incremented along a DBCS string by using a function similar to CharNext. The CharNext function is a DBCS processing function that is provided by the operating system and documented in the Platform SDK.

Sample Code

    UINT    Char;

    if (IsDBCSLeadByte(*pStr))
    {
        Char = PackDBCS(pStr[0], pStr[1]);
    }
    else
    {
        Char = pStr[0];
    }
    
    GetCharWidth32(hDC, Char, Char, pWidth);
				
The following partial list of functions are examples where this packing technique can be used to specify a DBCS character code.
  • GetCharABCWidths

REFERENCES

For more in-depth information regarding DBCS processing and other globalization issues, please see the following reference:

Developing International Software For Windows 95 and Windows NT, by Nadine Kano, Microsoft Press, ISBN# 1-55615-840-8

.

Modification Type:MinorLast Reviewed:5/17/2006
Keywords:kbDSWGDI2003Swept kbFont kbGDI kbhowto KB244046