获取字节streamios中的颜色值

我从二进制文件(.ase)读取这个字节stream粗体值表示颜色:

<41534546 00010000 00000005 c0010000 00140008 004f0070 0061006c 0074006f 006e0065 00000001 00000024 00080023 00320042 00420034 00300046 00005247 4220 * 3e2c acad3f34 b4b53d70 F0F1 * 0002 00010000 00240008 00230046 00460035 00420033 00310000 52474220 3f800000 3eb6b6b7 3e44c4c5 00020001 00000024 00080023 00460046 00380037 00340038 00005247 4220 * 3f80 00003f07 87883e90 9091 * 0002 00010000 00240008 00230030 00330038 00340043 00410000 52474220 3c40c0c1 3f048485 3f4acacb 0002>

如果颜色9BD6AE有字节块将是3f1b9b9c 3f56d6d7 3f2eaf

每个块表示每个颜色分量的浮点值。 (红色:0.607843绿色:0.839216蓝色:0.682353)

我如何从上述数据stream中获得这些浮点值?

假设你有NSData文件的内容,你可以将这些值转换为float ,如下所示:

 NSData *data = ... // contents of the ASE file const void *bytes = [data bytes]; int offset = ... // offset into the data stream (in bytes) // assuming offset is pointing to the start of 4 bytes that represent a float value: float colorComponent = [self floatFromBytes:bytes + offset]; // Helper method to convert bytes to a float // In ASE file, float values are 4-byte values in big-endian format - (float)floatFromBytes:(const void *)bytes { uint_32_t beVal = *(uint_32_t *)bytes; // data is in big-endian format uint_32_t locVal = CFSwapInt32BigToHost(beVal); // data in host format float res = *(float *)&locVal; // convert bytes to float return res; } 

这远不是一个完整的ASEparsingalgorithm。 但是这显示了如何将4字节值转换为float值。 您需要处理许多其他方面的ASE文件格式。