Search code examples
carraysintunsignedmemcpy

Copying an unsigned int array in C


I have to create an unsigned int array to make an IR remote message. To do this, I have to concatenate the header to the data bits, depending of which key I want to mimic. Here's what I have and the problem:

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

const unsigned HEADER[34] = {4608, 4398, 655, 1588, 655, 1588, 655, 1588, 655, 472, 655, 472, 655, 472, 655, 472, 655, 472, 655, 1588, 655, 1588, 655, 1588, 655, 472, 655, 472, 655, 472, 655, 472, 655, 472};

unsigned short BTN_VOL_UP = 0xE01F;

void intToBin(unsigned short a, char* buffer) {
    buffer += 15;
    int i = 15;
    for (i; i >= 0; i--) {
        *buffer-- = (a & 1) + '0';
        a >>= 1;
    }
}

void hexToData(unsigned** data, int code) {
  char* strHex = (char*) malloc(16 * sizeof(char));
  intToBin(code, strHex);
  int i = 0;
  for(i; i < 16; i++) {
    char c = strHex[i];
    *(*data)++ = 655;
    if (c == '1') {
      *(*data)++ = 1588;
    } else {
      *(*data)++ = 472;
    }
  }
  (*data) -= 32;
}

void getCode(unsigned** data, short code) {
  int i = 0;
  for (i; i < 34; i++) {
    *(*data)++ = HEADER[i];
  }
  unsigned* bits = (unsigned*) malloc(32 * sizeof(unsigned));
  hexToData(&bits, code);
  i = 0;
  for (i; i < 32; i++) {
    *(*data)++ = bits[i];
  }
  *(*data)++ = 647;
  *(*data)++ = 0;
  (*data) -= 68;
}

And here's the code calling all this:

unsigned* data = (unsigned*) calloc(68, sizeof(unsigned));
getCode(&data, BTN_VOL_UP);

After that, I print the HEADER (using a for to print each value) and the data variable, but that's what I get:

4608 4398 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472

0 136 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 655 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 1588 655 1588 647 0 16

The HEADER is correct, but the data is all messed up: the header in the data is completely wrong, the actually data part (the VOL_UP code) is right, but there's an extra value on the end that shouldn't be there: 16. the array should end with ...647, 0}.

Any ideas what's wrong with the code?

EDIT: I found the problem, edited the code to include corrections. Turns out that I was having problem finding the exact location to copy the values, so I decided to manually copy int by int and subtract the pointer at the end. Ugly, but it works.


Solution

  • Your code produces following...

    4608 4398 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 1588 1588 1588 655 655 655 655 655 655 655 655 1588 1588 1588 1588 1588 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 647 0
    

    Seems header is ok. I don't know algorithm for key generation, so I can't tell you if values 34-49 are ok, but you definitely have problem in 50-65 range. Your code never sets them to any value. Usually malloc doesn't initalize memory (it depends on standard library implementation tho), so you will get garbage in 50-65.