-3

I want to make a function that converts unsigned char to unsigned int and store it into an array. However, this ends up with an error that says

passing argument 1 of 'sprintf' from incompatible pointer type.

int main(void) {
    unsigned char key[16] = "1234567812345678";
    phex(key, 16); //store into an array here
}

uint64_t* phex(unsigned char* string, long len)
{
    uint64_t hex[len];
    int count = 0;

    for(int i = 0; i < len; ++i) {
        count = i * 2;
        sprintf(hex + count, "%.2x", string[i]);
    }

    for(int i = 0; i < 32; i++)
        printf(hex[i]);

    return hex;
}
Toby
  • 9,696
  • 16
  • 68
  • 132
vivola
  • 11
  • 1
  • 1
  • 4
  • You want array of 16 integer elements, and each element holds only one digit? – Rorschach Dec 02 '16 at 09:58
  • 3
    `sprintf` converts integers to a character representation, not the other way around – M.M Dec 02 '16 at 09:59
  • A [good reference for ***both*** `sprintf` and `printf`](http://en.cppreference.com/w/c/io/fprintf) should be helpful. – Some programmer dude Dec 02 '16 at 10:00
  • you can only `sprintf` to `char *` – Jean-François Fabre Dec 02 '16 at 10:00
  • 2
    You also have a much worse problem than the `printf`and `sprintf` errors: You return a pointer to a local variable. Once the function returns the array `hex` no longer exists, and the pointer you return will be invalid. – Some programmer dude Dec 02 '16 at 10:01
  • As for the problem you are trying to solve, is the goal to convert each digit in the input string into an actual one-digit number? Then [this ASCII table](http://en.cppreference.com/w/c/language/ascii) should give you a hint on how to convert a single digit character into a number. For some experimenting, try printing the decimal value (`printf` format `"%d"`) of `'3' - '0'`. – Some programmer dude Dec 02 '16 at 10:04
  • Also, you never assign return value (which is invalid, as mentioned before) to anything... – Rorschach Dec 02 '16 at 10:05

1 Answers1

1

As comments have already said, you have problems in your code... First of all sprintf function does totally opposite thing of what you want/expect it to do. Next, you create a local variable in your function, and return pointer to it.. As soon as function exits, pointer is invalid. Third problem I see is that you never assign return value to anything...

Proposition on how to fix your code:

unsigned* phex(unsigned char* string, long len);

int main(void) {
    int i;
    unsigned char key[16] = "1234567812345678";

    unsigned* ints = phex(key,16); //store into an array here

    for(i = 0; i < 16; i++)
        printf("%d ", ints[i]);

    //never forget to deallocate memory
    free(ints);

    return 0;
}

unsigned* phex(unsigned char* string, long len)
{
    int i;
    //allocate memory for your array
    unsigned* hex = (unsigned*)malloc(sizeof(unsigned) * len);

    for(i = 0; i < len; ++i) {
        //do char to int conversion on every element of char array
        hex[i] = string[i] - '0';
    }

    //return integer array
    return hex;
}
Rorschach
  • 734
  • 2
  • 7
  • 22
  • Although `unsigned` is the same as `unsigned int`, the latter is clearer. Also you should not cast the result of malloc http://stackoverflow.com/a/605858/4805077 – frostblue Dec 02 '16 at 10:19
  • @LudaOtaku Agreed on `unsigned int`. I even wrote it like that the first time and then modified it to make it shorted. Seems more clear to me this way. Thank you for the tip about `malloc`! – Rorschach Dec 02 '16 at 10:24
  • I used to write `unsigned` alone, it's mostly a matter of taste anyway, but it makes reading the code more pleasant to have that small `int` next to it IMO. And you're welcome about malloc ! `void *` pointers are a very interesting aspect of C. You probably don't want to cast pointers anyway, there's always the risk to truncate the address, and that means troubles on the horizon. – frostblue Dec 02 '16 at 10:33