我是Obj-C和cocoa的新程序员。我正在尝试编写一个用于读取二进制文件的框架(灵活的图像传输系统或FITS二进制文件,通常由天文学家使用)。我有兴趣提取的二进制数据可以有多种格式,我通过读取FITS文件的标题来获取其属性。
到目前为止,我设法创建了一个类来存储FITS文件的内容,并将标头隔离到一个NSString
对象中,并将二进制数据隔离到一个NSData
对象中。我还设法编写了一种方法,该方法允许我从标题中提取对解释二进制数据非常有价值的键值。
我现在正在尝试将NSData
对象转换为原始数组(数组double
,int
... short
)。但是,在这里,我被卡住了,希望能得到任何帮助。
根据我拥有的关于FITS文件的文档,我有 5 种可能性来根据BITPIX
键的值来解释二进制数据:
BITPIX value | Data represented
8 | Char or unsigned binary int
16 | 16-bit two's complement binary integer
32 | 32-bit two's complement binary integer
64 | 64-bit two's complement binary integer
-32 | IEEE single precision floating-point
-64 | IEEE double precision floating-point
我已经编写了如下所示的代码,以尝试将其转换NSData
为原始数组。
// self reefer to my FITS class which contain a NSString object
// with the content of the header and a NSData object with the binary data.
-(void*) GetArray
{
switch (BITPIX)
{
case 8:
return [self GetArrayOfUInt];
break;
case 16:
return [self GetArrayOfInt];
break;
case 32:
return [self GetArrayOfLongInt];
break;
case 64:
return [self GetArrayOfLongLong];
break;
case -32:
return [self GetArrayOfFloat];
break;
case -64:
return [self GetArrayOfDouble];
break;
default:
return NULL;
}
}
// then I show you the method to convert the NSData into a primitive array.
// I restrict my example to the case of 'double'. Code is similar for other methods
// just change double by 'unsigned int' (BITPIX 8), 'short' (BITPIX 16)
// 'int' (BITPIX 32) 'long lon' (BITPIX 64), 'float' (BITPIX -32).
-(double*) GetArrayOfDouble
{
int Nelements=[self NPIXEL]; // Metod to extract, from the header
// the number of element into the array
NSLog(@"TOTAL NUMBER OF ELEMENTS [%i]\n",Nelements);
//CREATE THE ARRAY
double (*array)[Nelements];
// Get the total number of bits in the binary data
int Nbit = abs(BITPIX)*GCOUNT*(PCOUNT + Nelements); // GCOUNT and PCOUNT are defined
// into the header
NSLog(@"TOTAL NUMBER OF BIT [%i]\n",Nbit);
int i=0;
//FILL THE ARRAY
double Value;
for(int bit=0; bit < Nbit; bit+=sizeof(double))
{
[Img getBytes:&Value range:NSMakeRange(bit,sizeof(double))];
NSLog(@"[%i]:(%u)%.8G\n",i,bit,Value);
(*array)[i]=Value;
i++;
}
return (*array);
}
但是,我在循环中打印的值与预期值有很大不同(使用官方 FITS 软件比较)。因此,我认为Obj-C double
不使用IEEE-754约定以及Obj-C int
不是二元补码。我真的不熟悉这两个约定(IEEE和twos-complement),并且想知道如何使用Obj-C进行这种转换。
提前非常感谢您提供的任何帮助或信息。