C program to convert decimal to hexadecimal
C program to convert decimal to hexadecimal
Converting decimal numbers to hexadecimal is a common task in computer programming, particularly when dealing with low-level systems or working with memory addresses. In this article, we will explore how to write a C program that performs this conversion.
We will provide a step-by-step explanation of the conversion process, discuss the underlying theory, and present a detailed implementation of the program.
Introduction
Converting decimal numbers to hexadecimal numbers is an important skill for programmers, especially those working with embedded systems, assembly language, or low-level programming.
Hexadecimal representation is widely used in these domains due to its compactness and compatibility with binary representations. In this article, we will guide you through the process of writing a C program that converts decimal numbers to their hexadecimal equivalents. We will explain the theory behind the conversion and provide a detailed implementation of the program to ensure a clear understanding of the concept.
Decimal to Hexadecimal Conversion
Before diving into the program's implementation, let's understand the theory behind decimal to hexadecimal conversion. The decimal number system uses base 10, meaning each digit's value represents a multiple of powers of 10. In contrast, the hexadecimal number system uses base 16, where each digit represents a multiple of powers of 16.
Hexadecimal digits range from 0 to 9, just like in the decimal system, but they are extended to include the letters A to F. The letter A corresponds to 10, B to 11, C to 12, and so on, up to F representing 15.
To convert a decimal number to hexadecimal, we follow these steps:
These steps are used to change a decimal number to hexadecimal:
- Multiply the decimal value by 16.
- Write down the final digit of the hexadecimal number, which will range from 0 to 15, as the least significant digit (rightmost digit).
- Once more divide the quotient from step 1 by 16.
- The final digit of the hexadecimal number should be written down.
- Keep going through steps 3 and 4 until the quotient is zero.
- The resulting remainders, when read backwards, combine to create the decimal number's hexadecimal representation.
Program Implementation
Now let's move on to the implementation of the C program. We will use a simple iterative approach that follows the conversion steps we discussed earlier.
First, we need to include the necessary header file, <stdio.h>, to enable input/output operations in our program.
#include <stdio.h>
void decimalToHexadecimal(int decimal) {
int remainder, quotient;
int hexadecimal[100];
int i = 0;
quotient = decimal;
while (quotient != 0) {
remainder = quotient % 16;
hexadecimal[i++] = remainder;
quotient = quotient / 16;
}
printf("Hexadecimal representation: ");
for (int j = i - 1; j >= 0; j--) {
if (hexadecimal[j] < 10)
printf("%d", hexadecimal[j]);
else
printf("%c", hexadecimal[j] + 55);
}
}
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToHexadecimal(decimal);
return 0;
}
Output:
![C program to convert decimal to hexadecimal](https://static.tutorialandexample.com/c/c-program-to-convert-decimal-to-hexadecimal1.png)
Let's go through the implementation step by step:
The decimalToHexadecimal function takes an integer decimal as input, representing the decimal number to be converted. This function will perform the conversion and print the hexadecimal representation.
Inside the function, we declare the variables remainder, quotient, and an integer array hexadecimal to store the hexadecimal digits.
We initialize i to 0, representing the index of the hexadecimal array.
The quotient variable is assigned the value of the input decimal.
Using a while loop, we repeatedly divide the quotient by 16 and store the remainder in the hexadecimal array.
After the loop terminates (when the quotient becomes zero), we move to the next step.
We iterate through the hexadecimal array in reverse order using a for loop.
If the value of the current digit is less than 10, we print it as a digit using %d in printf.
If the value is greater than or equal to 10, we convert it to the corresponding letter representation by adding 55 to it (the ASCII code offset of 'A' - 10). We then print it as a character using %c in printf.
In the main function, we prompt the user to enter a decimal number and read it using scanf. Then, we call the decimalToHexadecimal function, passing the user-input decimal value.