Sunday 20 November 2011

How do you assign a hexadecimal value to a variable? in C programming

How do you assign a hexadecimal value to a variable?

The C language supports binary, octal, decimal, and hexadecimal number systems. In each case, it is necessary to assign some sort of special character to each numbering system to differentiate them. To denote a binary number, use b at the end of the number (1101b). To denote an octal number, use the backslash character (\014). To denote a hexadecimal number, use the 0x character sequence (0x34). Of course, decimal is the default numbering system, and it requires no identifier.

To assign a hexadecimal value to a variable, you would do as shown here:
int x;
x = 0x20; /* put hex 20 (32 in decimal) into x
x = ‘0x20’; /* put the ASCII character whose value is
hex 20 into x */

You must know the hexadecimal numbering system in order to know what numbers to assign. Refer to FAQ
XX.24 for detailed information on the hexadecimal numbering system.

Cross Reference:

XX.24: What is hexadecimal?

No comments:

Post a Comment