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

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

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 Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.