Submitted by tushar pramanick on Tue, 03/05/2013 - 15:24

Converting to Hex Numbers
The difference between a decimal number and a hexadecimal number is that the hexadecimal is a base-16 numbering system. A hexadecimal number can be represented by four bits. (24 is equal to 16, which means four bits can produce 16 unique numbers.) Hexadecimal is often written as hex for short.

The hexadecimal numbers 0 through 9 use the same numeric symbols founded in the decimal numbers 0 through 9. uppercase A, B, C, D, E, and F are used to represent, respectively, the hexadecimal numbers 10 through 15. (Similarly, in lowercase, a, b, c, d, e, and f are used to represent these hex numbers.)

Listing 5.5 provides an example of converting decimal numbers to hex numbers by using %x or %X in the printf() function.

 

TYPE
Listing 5.5. Converting to hex numbers.

 

1:  /* 05L05.c: Converting to hex numbers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     printf("Hex(uppercase)    Hex(lowercase)    Decimal\n");
7:     printf("%X                %x                %d\n", 0, 0, 0);
8:     printf("%X                %x                %d\n", 1, 1, 1);
9:     printf("%X                %x                %d\n", 2, 2, 2);
10:    printf("%X                %x                %d\n", 3, 3, 3);
11:    printf("%X                %x                %d\n", 4, 4, 4);
12:    printf("%X                %x                %d\n", 5, 5, 5);
13:    printf("%X                %x                %d\n", 6, 6, 6);
14:    printf("%X                %x                %d\n", 7, 7, 7);
15:    printf("%X                %x                %d\n", 8, 8, 8);
16:    printf("%X                %x                %d\n", 9, 9, 9);
17:    printf("%X                %x                %d\n", 10, 10, 10);
18:    printf("%X                %x                %d\n", 11, 11, 11);
19:    printf("%X                %x                %d\n", 12, 12, 12);
20:    printf("%X                %x                %d\n", 13, 13, 13);
21:    printf("%X                %x                %d\n", 14, 14, 14);
22:    printf("%X                %x                %d\n", 15, 15, 15);
23:    return 0;
24: }

 

OUTPUT
The following output is obtained by running the executable file, 05L05.exe, on my machine:

 

C:\app> 05L05

    Hex(uppercase)   Hex(lowercase)   Decimal

    0                0                0

    1                1                1

    2                2                2

    3                3                3

    4                4                4

    5                5                5

    6                6                6

    7                7                7

    8                8                8

    9                9                9

    A                a                10

    B                b                11

    C                c                12

    D                d                13

    E                e                14

    F                f                15

    C:\app>

 

ANALYSIS
Don't panic when you see so many printf() functions being used in Listing 5.5. In fact, the program in Listing 5.5 is very simple. The program has just one function body from lines 5_23.

 

The printf() function in line 6 prints out a headline that contains three fields: Hex(uppercase), Hex(lowercase), and Decimal.

 

Then, lines 7_22 print out the hex and decimal numbers 0 through 15. Sixteen printf() functions are called to accomplish the job. Each of the printf() functions has a format string as the first argument followed by three integers as three expressions. Note that the hex format specifiers %X and %x are used within the format string in each of the printf() functions to convert the corresponding expressions to the hex format (both uppercase and lowercase).

 

In reality, nobody would write a program like the one in Listing 5.5. Instead, a loop can be used to call the printf() function repeatedly. Looping (or iteration) is introduced in Hour 7, "Doing the Same Thing Over and Over."

Related Items

The Basics of Disk File I/O

The Basics of Disk File I/O

Now let's focus on how to open and close a disk data file and how to interpret error messages returned by I/O functions.

Files Versus Streams

Files Versus Streams

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q What are the differences between a union and a structure?

Making Structures Flexible

Making Structures Flexible

The second application of unions is to nest a union inside a structure so that the structure can hold different types of values.