I am trying to run a sample code to convert an array of decimal number to hexa and store them in another array. The code works if I scanf the numbers but when using the array I am getting segmentation error.
#include <stdio.h>
int main(void)
{
int bin = 0;
// int num ;
int i = 0, rem;
char hex_arr[60];
//printf("Enter a decimal number: ");
//scanf("%d", &num);
int num [60] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 128, 31, 239, 31, 254, 1, 255, 240, 0, 0, 0, 0, 127, 255, 192, 255, 255, 255, 128, 127, 231, 192, 120, 14, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (int k = 0; k <= 59; i++)
{
rem = num[k] % 16; // get the right most digit
if (rem < 10)
{
hex_arr[i++] = 48 + rem;
}
else
{
hex_arr[i++] = 55 + rem;
}
num[k] /= 16; // get the quotient
}
printf("0x");
for(int j = i - 1; j >= 0 ; j--) // print the hex_arr in reverse order
{
printf("%c", hex_arr[j]);
}
return 0;
}