如何使用Cocoa Touch解析带有浮动(Java生成)的二进制文件?

如何使用Cocoa Touch解析带有浮动(Java生成)的二进制文件?,java,cocoa-touch,parsing,Java,Cocoa Touch,Parsing,给出了生成二进制文件的以下Java代码: DataOutputStream out = new DataOutputStream(new FileOutputStream("foo.dat")); out.writeInt(1234); out.writeShort(30000); out.writeFloat(256.384f); 我正在使用以下Objective-C代码并设法解析int和short值: NSString *path = [[NSBundle mainBundle] path

给出了生成二进制文件的以下Java代码:

DataOutputStream out = new DataOutputStream(new FileOutputStream("foo.dat"));
out.writeInt(1234);
out.writeShort(30000);
out.writeFloat(256.384f);
我正在使用以下Objective-C代码并设法解析int和short值:

NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"dat"];
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];

unsigned long intValue;
memcpy(&intValue, [[file readDataOfLength:4] bytes], 4);
intValue = NSSwapBigLongToHost(intValue);

unsigned short shortValue;
memcpy(&shortValue, [[file readDataOfLength:2] bytes], 2);
shortValue = NSSwapBigShortToHost(shortValue);

我现在的问题是浮点值:关于如何解析它有什么线索吗?

很抱歉,我不能提供完整的答案,但我可以说Java浮点值作为浮点值的四字节IEEE 754表示形式保存到文件中,如本文档所示:


这描述了保存的值的确切结构,因此您可以将其作为int读入,并手动将有效位和指数的位解析为浮点。似乎应该有一个库调用来解析打包成这种格式的int的浮点值。希望其他人知道处理此问题的库调用。

只需告诉我们
提供的函数和类型

请注意,在交换字节之前,它们使用一种特殊类型来保持浮点。这是因为,如果交换浮点值的字节,它可以被更改为一个信号NaN,在某些处理器上,这将通过将其分配给变量而导致硬件异常

NSSwappedFloat bigEndianFloat;
memcpy(&bigEndianFloat, [[file readDataOfLength:4] bytes], 4);
float floatValue = NSSwapBigFloatToHost(bigEndianFloat);