objective-c - 使用 ieee-754 或二进制补码将 NSData 转换为原始变量?

标签 objective-c cocoa binary ieee-754 twos-complement

我是 Obj-Ccocoa 的新程序员。我正在尝试编写一个用于读取二进制文件的框架(灵活图像传输系统FITS二进制文件,通常由天文学家使用)。我有兴趣提取的二进制数据可以有多种格式,我通过读取 FITS 文件的 header 来获取其属性。

到目前为止,我设法创建一个类来存储 FITS 文件的内容并将 header 隔离到 NSString 中。对象和二进制数据到 NSData目的。我还设法编写了一种方法,该方法允许我从 header 中提取关键值,这对于解释二进制数据非常有值(value)。

我现在正在尝试转换 NSData对象转换为原始数组(数组 doubleintshort ...)。但是,在这里,我陷入困境,希望得到任何帮助。

根据我拥有的有关 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不是二进制补码。我真的不熟悉这两个约定(IEEEtwos-complement),并且想知道如何使用 Obj-C 进行此转换强>.

提前非常感谢您的任何帮助或信息。

最佳答案

Objective-C 使用 IEEE-754 float (就像几乎每个系统一样)以及二进制补码整数。我认为你的推测是错误的。也许您遇到了字节顺序问题?

关于objective-c - 使用 ieee-754 或二进制补码将 NSData 转换为原始变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3026110/

相关文章:

c++ - 类对象成员的顺序对性能有影响吗?

python:解压缩 IBM 32 位 float

objective-c - TouchID 在某些 iPhone 5S 设备上崩溃

ios - 在数组中查找距用户位置最近的经度和纬度

ios - 在 iOS 应用程序中调用/测试 Web API

objective-c - 当 AXIsProcessTrusted() 为真时,重新启动 OS X 应用程序以执行 CGEventTapCreate()

macos - 为 OSX 菜单栏动态创建模板图像

objective-c - 如何在 Xcode 中弃用方法

c - 我如何将二进制转换为 ASCII

iphone - 添加为 subview 时 UITextField 不显示