iOS NSData转float,Data转Float,Byte Array转float

版权声明:本文为博主原创文章,未经博主允许可以转载,但转载时请附上原文地址: https://blog.csdn.net/youshaoduo/article/details/84879625

直接上代码:
OC版本的:

    Byte byte[] = {0x47,0x33,0x18,0x00};//{0x00,0x18,0x33,0x47};
    
    NSData * data = [NSData dataWithBytes:byte length:sizeof(byte)];
    int32_t bytes;
    [data getBytes:&bytes length:sizeof(bytes)];
    bytes = OSSwapBigToHostInt32(bytes);
    
    float number;
    memcpy(&number, &bytes, sizeof(bytes));
    NSLog(@"%f", number);

Swift版本的:

    @objc class func floatValue(data: Data) -> Float {
        return Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.pointee }))
    }

猜你喜欢

转载自blog.csdn.net/youshaoduo/article/details/84879625
今日推荐