I try to convert a decimal number to its its hexadecimal representation (and want to get only the fourth LSB digits).
It is seems that my function do works, but when i try to get the value of the number from the main function, i get all the digits and x1!=x2
.
Where do I wrong?
here is my code:
char* dec_to_hex_converter(int num, char x[])
{
int i = 1;
int size = 0;
sprintf(x, "%04X\n", num);
size = strlen(x);
if (size > 4)
{
while (size > 4)
{
x++;
size--;
}
x--;
}
*(x + 4) = '\0';
printf("x1=%s\n", x);
return x;
}
int main()
{
char x[10];
dec_to_hex_converter(-16,x);
printf("x2=%s\n", x);
}
I want to get FFF0
but when I print the result I get FFFFFFF0
(ONLY OUTSIDE THE FUNCTION)
thanks in advance