-1

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; 
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
AmgA
  • 1
  • 4

2 Answers2

1

You need to get rid of all "magic numbers".

  • k <= 59-> k < 60
  • 48 + rem -> '0' + rem
  • 55 + rem -> '7' + rem.

Wait, how does '7' make any sense? Turns out it doesn't & the use of "magic numbers" were hiding that bug. You probably meant 65, which should be written as 'A'.

(Strictly speaking, arithmetic using symbol values isn't well-defined in C, since only the symbols '0' to '9' are actually guaranteed to be a contiguous sequence. In practice "it will work" on all mainstream systems.)

As noted in comments, for (int k=0; ...; i++) probably doesn't make any sense either.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    I don't think that using the number `55` was a "bug". In OP's code, when that line of code is executed, the variable `rem` has a value between `10` and `15`, so the expression `55 + rem` evaluates to an ASCII code between `65` and `70`, i.e. between `'A'` and `'F'`. Therefore, that part of OP's code seems to work as intended. – Andreas Wenzel Jul 06 '21 at 12:47
  • 1
    Though `'A' + (rem - 10)` would be clearer. – ikegami Jul 06 '21 at 12:54
  • Oh well.... C uses karma extensively: write really weird code, get really weird results. At this point maybe just toss this whole snippet in the garbage and google "integer to hex string conversion c". Sensible solutions are likely contain a look-up table `"01234567890ABCDEF"`. – Lundin Jul 06 '21 at 13:01
  • The i++ was mistake I didn't notice changing to K++ everything works as it should. – AmgA Jul 06 '21 at 13:30
  • @AmgA: In future, you may want to run your code line by line in a [debugger](https://stackoverflow.com/q/25385173/12149471) while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended. That way, it should be easy for you to find such bugs yourself. – Andreas Wenzel Jul 06 '21 at 13:57
1

Would this help

Notes

  1. num is now defined as a unsigned char array - you can cast in ByteToASCII if this is a problem
  2. It converts the whole string in one go but it's easy to convert to handle one byte at a time
  3. it was unclear if you want the num array as a list of nybbles or as a whole byte but, again, that would be easy to change in this code
#include<stdio.h>
#include<string.h>
#include<ctype.h>

#include <stdio.h> 

void ByteToASCII (char * string, unsigned char   byte )
      {
         char    curval      ;

         curval = (char) ('0'  | (byte >> 4)) ;

         if ('9' < curval)
         {
            curval += (char) ('A' - '9' - 1) ;
         }

         *string++ = curval ;

         byte <<= 4 ;

         curval = (char) ('0'  | (byte >> 4)) ;

         if ('9' < curval)
         {
            curval += (char) ('A' - '9' - 1) ;
         }

         *string++ = curval ;
         *string = 0 ; // keep null terminated
      }

int main(void)
{   
    //printf("Enter a decimal number: "); 
    //scanf("%d", &num); 
    
    unsigned char 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};
    char hex_arr[(2 * sizeof(num)) + 1];

    for (int k = 0; k < sizeof(num); k++)
    {
        ByteToASCII (&hex_arr[k * 2], num[k]) ;
    }
    printf(hex_arr);
    return 0; 
}
Edgen
  • 69
  • 7