Basically, I have this function here that spits out a hexadecimal value when a decimal value is entered. This code works perfectly fine but when I insert it verbatim into a larger set of code I am working on, the hexadecimal number spit out has 8 extra unnecessary characters followed by the correct hexadecimal value. Any thoughts on how to fix it? I was told to terminate the string hexadecimal but where do I do that exactly and how?
#include <stdio.h>
#include <string.h>
int main(void){
int decimalnum, quotient, remainder;
int k, l = 0;
char hexadecimalnum[100];
char str[50];
int m;
char temp;
int j;
printf("enter decimal number: ");
scanf("%d", &decimalnum);
quotient = decimalnum;
while (quotient != 0) {
remainder = quotient % 16;
if(remainder < 10) {
hexadecimalnum[l++] = 48 + remainder;
}
else {
hexadecimalnum[l++] = 55 + remainder;
}
quotient = quotient / 16;
}
// Display integer to character.
for(k = l; k >= 0; k--) {
str[k] = hexadecimalnum[k];
}
m = 0;
j = l-1;
// Flip the string because it is written backwards.
while(m <j) {
temp = str[m];
str[m] = str[j];
str[j] = temp;
m++;
j--;
}
printf("\n%s\n", str);
return 0;