0

Ive been having trouble where the segmentation fault is in my code. I am trying to make a function that converts decimal numbers into hex numbers. Also if I was to use: 15&15 would the number be 1111 or 15?

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *to_hex(int n);

main(){
    int dec;
    printf("-> ");
    scanf("%d",dec);
    printf(to_hex(dec));
}

char *to_hex(int n){
    char *array = malloc(sizeof(int)*2+8);/*makes array*/
    int i;
    int index=0;/*used for storing into array*/
    for(i=28;i>-1;i=i-4){/*mask and compare 8 times*/
        int shift=n>>i;/*right shift*/
        int mask=shift&15;/*masks*/
        if (mask<10){
            *(array+index)=(char)mask;
            index++;
        }
        else{
            switch(mask){
            case 10:
                *(array+index)='a';
                index++;
                break;
            case 11:
                *(array+index)='b';
                index++;
                break;
            case 12:
                *(array+index)='c';
                index++;
                break;
            case 13:
                *(array+index)='d';
                index++;
                break;
            case 14:
                *(array+index)='e';
                index++;
                break;
            case 15:
                *(array+index)='f';
                index++;
                break;
            }
        }
    }
    *(array+index)='\0';
    return array;
}

I think the : int mask : would equal 15 and not 1111 am I correct? Thanks

josh chang
  • 13
  • 1
  • 2
  • `*(array+index)='a'; index++;` is **much rather** `array[index++] = 'a';` (use whitespace and C idioms...) –  Oct 28 '13 at 16:24
  • `15&15 would the number be 1111 or 15?` - 1111 in binary, 15 in decimal... I don't understand what your problem with all of this conversion is. Also, you got the character wrong if `mask` is less than 10. The character with code 0 is not the character `'0'`. You will need `mask + '0'` for that. –  Oct 28 '13 at 16:24
  • You could just replace `printf(to_hex(dec));` with `printf("%x",dec);` – r3mainer Oct 28 '13 at 16:26
  • I just started the language C, so im not too sure what c idioms mean, but I am trying to learn pointers instead of using "array[]". I am just a bit confused as to where the segmentation fault lies. I also forgot to put in the code n=n<<4 after the else statement in order to prepare for the next hex digit. – josh chang Oct 28 '13 at 16:30
  • I am not sure what you mean by code 0. Do you mean when i cast the integer to a char? – josh chang Oct 28 '13 at 16:36
  • @joshchang Yes, I mean that. –  Oct 28 '13 at 17:12

1 Answers1

0

You can probably avoid the segfault by changing scanf("%d",dec); to scanf("%d",&dec);, and if you want printable results, change *(array+index)=(char)mask; to *(array+index)=(char)mask+'0'; (or, better, array[index] = mask + '0';

printf(to_hex(dec)); is a bit iffy, too. Use printf("%s",to_hex(dec)); instead; it's safer.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
  • oh wow thank you so much. I wish I can upvote this, but I dont have enough rep. Thank you again ^^ – josh chang Oct 28 '13 at 16:51
  • No probs. Have fun programming — it'll all start to make sense before long. If you can afford it, get a copy of this book: http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628 – r3mainer Oct 28 '13 at 16:55