如何在Objective c中将4个uint8_t数组元素转换为float?
我尝试了移位运算符,它似乎不起作用:(
答案 0 :(得分:4)
您是否正在考虑未定义的内容?:
union U8f {
uint8_t byte[4];
float f;
};
...
union U8f u8f;
u8f.byte[0] = ...
u8f.byte[1] = ...
...
float f = u8f.f;
请记住,字节顺序很重要。我会退后一步,等待当之无愧的批评。 ; - )
答案 1 :(得分:0)
与普通老C一样,投下它:
float f = (float) intArray[x];
完整示例:
#import <Foundation/Foundation.h>
int
main(int argc, char** argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
uint8_t ints[4] = { 1, 2, 3, 4 };
float floats[4];
floats[0] = (float) ints[0];
floats[1] = (float) ints[1];
floats[2] = (float) ints[2];
floats[3] = (float) ints[3];
NSLog(@"floats[0]: %f", floats[0]);
NSLog(@"floats[1]: %f", floats[1]);
NSLog(@"floats[2]: %f", floats[2]);
NSLog(@"floats[3]: %f", floats[3]);
[pool release];
}