IEE754 和double类型互转

.h文件

#import <Foundation/Foundation.h>


union FloatingPointSinglePrecisionIEEE754 {

    struct {

        unsigned int mantissa : 23;

        unsigned int exponent : 8;

        unsigned int sign : 1;

    } raw;

    float f;

} fnumber;


union FloatingPointDoublePrecisionIEEE754 {

    struct {

        uint64_t mantissa : 52;

        uint64_t exponent : 11;

        uint64_t sign : 1;

    } raw;

    double d;

} dnumber;


@interface IEEE754DoubleConvert : NSObject


+ (NSData *)double_to_data:(double)value;


+ (double)data_to_double:(NSData *)data;


+ (NSData *)string_to_data:(NSString *)destString;


+ (NSString *)data_to_string:(NSData *)destData;


+ (void)bytearrtostr:(Byte *)data length:(int)length;


double bitstring_to_double(const char* p);


@end



.m文件

@implementation IEEE754DoubleConvert


double bitstring_to_double(const char* p)

{

    unsigned long long x = 0;

    for (; *p; ++p)

    {

        x = (x << 1) + (*p - '0');

    }

    double d;

    memcpy(&d, &x, 8);

    return d;

}


+ (NSData *)double_to_data:(double)value{

    dnumber.d = value;

    uint64_t result = 0x0000000000000000;

    uint64_t mantissa = dnumber.raw.mantissa;

    uint64_t exponent = dnumber.raw.exponent;

    uint64_t sign = dnumber.raw.sign;

    result = result | (sign << 63);

    result = result | (exponent << 52);

    result = result | mantissa;

    UInt8   result8          = result & 0x00000000000000ff;

    UInt8   result7          = (result & 0x000000000000ff00) >> 8;

    UInt8   result6          = (result & 0x0000000000ff0000) >> 16;

    UInt8   result5          = (result & 0x00000000ff000000) >> 24;

    UInt8   result4          = (result & 0x000000ff00000000) >> 32;

    UInt8   result3          = (result & 0x0000ff0000000000) >> 40;

    UInt8   result2          = (result & 0x00ff000000000000) >> 48;

    UInt8   result1          = (result & 0xff00000000000000) >> 56;

    NSMutableData *data = [NSMutableData dataWithBytes:&result1 length:sizeof(result1)];

    [data appendBytes:&result2 length:sizeof(result2)];

    [data appendBytes:&result3 length:sizeof(result3)];

    [data appendBytes:&result4 length:sizeof(result4)];

    [data appendBytes:&result5 length:sizeof(result5)];

    [data appendBytes:&result6 length:sizeof(result6)];

    [data appendBytes:&result7 length:sizeof(result7)];

    [data appendBytes:&result8 length:sizeof(result8)];

    return data;

}


+ (double)data_to_double:(NSData *)data{

    long currentLocation = 0;

    UInt64 destU;

    UInt8 data1;

    [data getBytes:&data1 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = data1;

    currentLocation += sizeof(UInt8);

    UInt8 data2;

    [data getBytes:&data2 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data2;

    currentLocation += sizeof(UInt8);

    UInt8 data3;

    [data getBytes:&data3 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data3;

    currentLocation += sizeof(UInt8);

    UInt8 data4;

    [data getBytes:&data4 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data4;

    currentLocation += sizeof(UInt8);

    UInt8 data5;

    [data getBytes:&data5 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data5;

    currentLocation += sizeof(UInt8);

    UInt8 data6;

    [data getBytes:&data6 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data6;

    currentLocation += sizeof(UInt8);

    UInt8 data7;

    [data getBytes:&data7 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data7;

    currentLocation += sizeof(UInt8);

    UInt8 data8;

    [data getBytes:&data8 range:NSMakeRange(currentLocation, sizeof(UInt8))];

    destU = (destU << 8) | data8;

    UInt64 rsign = destU>>63;

    UInt64 rexponent = (destU>>52) & 0x7ff;

    UInt64 rmantissa = destU & 0x000fffffffffffff;

    dnumber.raw.mantissa = rmantissa;

    dnumber.raw.exponent = rexponent;

    dnumber.raw.sign = rsign;

    return dnumber.d;

}


+ (NSData *)string_to_data:(NSString *)destString{

    return [destString dataUsingEncoding:NSUTF8StringEncoding];

}


+ (NSString *)data_to_string:(NSData *)destData{

    return [NSString stringWithUTF8String:destData.bytes];

}


+ (void)bytearrtostr:(Byte *)data length:(int)length

{

    char char_1 = '1',char_0 = '0';

    char *chars = malloc(length*8+1);

    chars[length*8] = '\n';

    for(int i=0;i<length;i++)

    {

        Byte bb = data[i];

        for(int j=0;j<8;j++)

        {

            if(((bb>>j)&0x01) == 1)

            {

                chars[i*8+j] = char_1;

            }else{

                chars[i*8+j] = char_0;

            }

        }

        char temp = 0;

        temp =  chars[i*8+0];chars[i*8+0] = chars[i*8+7];chars[i*8+7] = temp;

        temp =  chars[i*8+1];chars[i*8+1] = chars[i*8+6];chars[i*8+6] = temp;

        temp =  chars[i*8+2];chars[i*8+2] = chars[i*8+5];chars[i*8+5] = temp;

        temp =  chars[i*8+3];chars[i*8+3] = chars[i*8+4];chars[i*8+4] = temp;

    }

    NSString *string = [NSString stringWithCString:chars encoding:NSUTF8StringEncoding];

    NSLog(@"%@",string);

    free(chars);

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值