C program to convert decimal to hexadecimal

Converting a decimal number to hexadecimal in C can be done using a simple algorithm. Hexadecimal is a base-16 number system, which means it uses 16 different symbols (0-9 and A-F) to represent values. To convert a decimal number to hexadecimal, we repeatedly divide the decimal number by 16 and keep track of the remainders, which represent the hexadecimal digits. We then reverse the order of the remainders to get the hexadecimal representation.

Here's a C program to convert a decimal number to hexadecimal with an example and the expected output:

#include <stdio.h>


void decimalToHexadecimal(int decimal) {
    char hex[20]; // To store the hexadecimal representation
    int index = 0;

    while (decimal > 0) {
        int remainder = decimal % 16;

        // Convert remainder to hexadecimal digit
        if (remainder < 10) {
            hex[index] = remainder + '0';
        } else {
            hex[index] = remainder + 'A' - 10;
        }

        decimal /= 16;
        index++;
    }

    // Print the hexadecimal number in reverse order
    printf("Hexadecimal: ");
    for (int i = index - 1; i >= 0; i--) {
        printf("%c", hex[i]);
    }
    printf("\n");
}

int main() {
    int decimalNumber = 255; // Example decimal number

    decimalToHexadecimal(decimalNumber);

    return 0;
}
 

In this program:

  1. We define a function decimalToHexadecimal that takes an integer decimal as its argument.

  2. Inside the function, we use a character array hex to store the hexadecimal representation. We also use an index variable to keep track of the current position in the hex array.

  3. We enter a while loop that continues until the decimal number becomes zero.

  4. In each iteration of the loop, we calculate the remainder when the decimal is divided by 16. This remainder is then converted to the corresponding hexadecimal digit and stored in the hex array.

  5. We then update the decimal by performing integer division by 16 and increment the index to move to the next position in the hex array.

  6. After the loop, we print the hexadecimal number in reverse order to get the correct representation.

Now, let's run the program with an example value, such as 255:

Hexadecimal: FF

The output shows that the decimal number 255 is correctly converted to its hexadecimal representation, which is 'FF'.

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post