views:

27

answers:

1

Using foundation and cocoa frameworks on Mac, I am trying to convert an NSData object in humanly understandable number. Let say the NSData object is an image of NPIXEL. I know the binary data are coded in big endian and represent 32 bit integer (to be more precise 32 bit two complements integer). I write the piece of code bellow to convert the NSData into an int array. But the value I got are completely wrong (this does not means the measurement are bad, I used a special software to read the data and the value given by the software are different from the one I got with my code).

-(int *) GetArrayOfLongInt
{
  //Get the total number of element into the Array
  int Nelements=[self NPIXEL];
  //CREATE THE ARRAY
  int array[Nelements];
  //FILL THE ARRAY
  int32_t intValue;
  int32_t swappedValue;
  double Value;
  int Nbit = abs(BITPIX)*GCOUNT*(PCOUNT + Nelements); Nbit/=sizeof(int32_t);
  int i=0;
  int step=sizeof(int32_t);
  for(int bit=0; bit < Nbit; bit+=step)
  {
    [Img getBytes:&swappedValue range:NSMakeRange(bit,step)];
    intValue= NSSwapBigIntToHost(swappedValue);
    array[i]=intValue;
    i++;
  }
  return array;
}

This piece of code (with minor change) work perfectly when the binary data represent float or double, but I dont when it is 16,32 or 64 bit integer. I also tried changingNSSapBigIntToHostintoNSSwapLittleInttoHost`. I even tried with long, but the results is still the same, I got bad values. What wrong I am doing ?

PS: Some of the variable in my code are already set elsewhere in my program. BITPIX is the bit size of each pixel. In this case 32. GCOUNT is equal to 1, PCOUNT 0 and Nelements is the total number of pixel I should have in my image.

A: 

Returning a pointer to a local variable is a very bad idea. array could get overwritten at any time (or if you were to write through the pointer, you could corrupt the stack). You probably want something like:

// CREATE THE ARRAY
int *array = malloc(Nelements * sizeof(int));

Your algorithm seems a bit overkill, too. Why not just copy out the whole array from the NSData object, and then byteswap the entries in place? Something like:

int32_t length = [Img length];
int32_t *array = malloc(length);

[Img getBytes:array length:length];

for (i = 0; i < length/sizeof(int32_t); i++)
{
    array[i] = NSSwapBigIntToHost(array[i]);
}
Carl Norum
Thanks for the advice, you are right, it look more clean. But little question, you create an array of the size of `length` but in reality you only fill `length/sizeof(int_32)`case of your array. Should not you do `int32_t *array=malloc(length/sizeof(int_32));`
William GILLARD
@William, no, the array has to be `length` bytes long. If you divide, you'll end up with array that's only 25% of the size you need. The code I showed there allocates `length` bytes, which is space for `length/sizeof(int32_t)` integers.
Carl Norum
QUOTE :"Returning a pointer to a local variable is a very bad idea."I agree. But, I tried several thing, as`int (*array)[Nelements]; ... return *array;`but, compiler is not so happy. In reality he is not so happy with `retrun array;`also. But, I was mainly focusing on trying to correctly convert the NSdata before solve this issue.
William GILLARD
@William, calling `malloc` as in my example code will fix all of those problems.
Carl Norum
@Carl, Yes, true, malloc alloc byte size to the array, not the number of element of the array. Thanks for this advice. I will update my code.
William GILLARD