In: Computer Science
A C program that going to accept a single command line arg and convert it to hexadecimal as an array of char that has 16 bits. The array should contain the hex of the int argument. Side note the command line arg is a valid signed int and the program should be able to convert negative numbers.
According to the Question -
here is the program that convert your number in HEXADECIMAL
#include<stdio.h>
#include<conio.h>
// function to convert decimal to hexadecimal
void decToHexa(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--)
printf("%c",hexaDeciNum[j]);
}
// Driver program to test above function
int main()
{
int n;
printf("Enter a Number to Convert - ");
scanf("%d",&n);
decToHexa(n);
return 0;
}
OUTPUT : -