The information in this article applies to:
SUMMARYWhen you try to display a signed character in two-digit hexadecimal format as follows:
four digits are displayed if the character has a value of 0x80 to 0xFF
because of promotion of the character to a signed int. Under Windows NT,
eight digits will be displayed because of the integer size difference. The
%x and %X specifiers designate an unsigned hexadecimal integer, but because
leading 0s (zeros) are dropped, values in the range of 0 to 127 produce the
desired two digits. See the sample code below.
To preserve the two-digit display for all possible values, typecast the character to an unsigned character in the printf() argument list, as shown below:
Another way to achieve this, if sign is of no consequence, is to declare
the variables as unsigned character.
MORE INFORMATION
For ANSI compliance, the compiler promotes all character arguments to
signed int. If the type is signed character, then the value will be sign-
extended. Thus, the int will be negative if the left-most bit of the
character was set, resulting in a different value for the int. When an
unsigned character is promoted, a 0 (zero) is placed in the high byte so
that the value is retained.
Sample Code
Additional query words: conversion
Keywords : kbcode kbLangC kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600 |
Last Reviewed: July 1, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |