美文网首页
数据转换

数据转换

作者: wayne0207 | 来源:发表于2019-05-06 15:51 被阅读0次

    //

    //  DataTransfrom.h

    // 

    //

    //  Created by Wayne on 2019/4/8.

    //  Copyright © 2019 Wayne. All rights reserved.

    //

    #import

    @interfaceDataTransfrom :NSObject

    + (float)getFloatValue:(NSString*)hexString ;

    + (float)getFloatHighToLow:(NSString*)hexString;

    + (float)getJumpFloat:(NSString*)hexString ;

    + (int)getIntValue:(NSString*)hexString ;

    + (NSString*)getHexString:(float)data ;

    + (NSString*)getHexStringfromInt:(int)data ;

    + (NSData*)getData:(NSString*)hexString andSplit:(NSString*)splitSign ;

    @end


    //

    //  DataTransfrom.m

    // 

    //

    //  Created by Wayne on 2019/4/8.

    //  Copyright © 2019 Wayne. All rights reserved.

    //

    #import "DataTransfrom.h"

    @implementation DataTransfrom

    + (float)getFloatValue:(NSString*)hexString

    {

        floatvalue =  0;

        NSScanner *scanner = [NSScanner scannerWithString:hexString] ;

        unsigned long long longValue ;

        [scanner scanHexLongLong: &longValue] ;

        long decimal = (long)pow(2,23) ;

        long long threshold = (long)powl(2,31) ;

        long high = (long)(longValue/decimal) -127;

        long low = longValue%decimal ;

        long singlevalue = low + decimal ;

        long clacvalue = singlevalue*powl(2, high) ;

        value = (float)(clacvalue)/decimal ;

        if (longValue >= threshold) {

            value *= -1;

        }

        returnvalue ;

    }

    + (float)getJumpFloat:(NSString*)hexString

    {

        float value =  0;

        NSString*strOne = [hexString substringWithRange:NSMakeRange(0,2)] ;

        NSString *strTwo = [hexString substringWithRange:NSMakeRange(2,2)] ;

        NSString *strThree = [hexString substringWithRange:NSMakeRange(4,2)] ;

        NSString *strFour = [hexString substringWithRange:NSMakeRange(6,2)] ;

        NSScanner *scanner = [NSScanner scannerWithString: [NSStringstringWithFormat:@"%@%@%@%@",strFour,strThree,strTwo,strOne]] ;

        unsigned long long longValue ;

        [scanner scanHexLongLong:&longValue] ;

        long decimal = (long)pow(2,23) ;

        long longthreshold = (long)powl(2,31) ;

        long high = (long)(longValue/decimal) -127;

        long low = longValue%decimal ;

        long singlevalue = low + decimal ;

        long clacvalue = singlevalue*powl(2, high) ;

        value = (float)(clacvalue)/decimal ;

        if (longValue >= threshold) {

            value *= -1;

        }

        returnvalue ;

    }

    + (float)getFloatHighToLow:(NSString*)hexString

    {

        float value =  0;

        NSString *strOne = [hexString substringWithRange:NSMakeRange(0,2)] ;

        NSString *strTwo = [hexString substringWithRange:NSMakeRange(2,2)] ;

        NSString *strThree = [hexString substringWithRange:NSMakeRange(4,2)] ;

        NSString *strFour = [hexString substringWithRange:NSMakeRange(6,2)] ;

        NSScanner *scanner = [NSScanner scannerWithString:[NSStringstringWithFormat:@"%@%@%@%@",strFour,strThree,strTwo,strOne]] ;

        unsigned long long longValue ;

        [scanner scanHexLongLong: &longValue] ;

        long decimal = (long)pow(2,23) ;

        long longthreshold = (long)powl(2,31) ;

        long high = (long)(longValue/decimal) -127;

        long low = longValue%decimal ;

        long singlevalue = low + decimal ;

        long clacvalue = singlevalue*powl(2, high) ;

        value = (float)(clacvalue)/decimal ;

        if(longValue >= threshold)    {

            value *= -1;

        }

        returnvalue ;

    }

    + (NSString*)getHexString:(float)data

    {

        NSString *hexString =@"";

        long decimal = powl(2,23) ; 

        long longthreshoid = powl(2,31) ;

        BOOL isNegative = data >0?NO:YES;

        if (isNegative) {

            data *= -1;

        }

        float ratio =1;

        while (data <1|| data >=2) {

            if(data <1) {

                data *=2;

                ratio /=2;

            }

            if(data >=2) {

                data = data/2;

                ratio *=2;

            }

        }

        long lowdata = (data -1)*decimal ;

        intpowTimes =0;

        while (ratio !=1) {

            if (ratio >1) {

                ratio /=2;

                powTimes +=1;

            }else{

                ratio *=2;

                powTimes -=1;

            }

        }

        long highdata = powTimes +127;

        long longtotaldata = highdata * decimal + lowdata ;

        if(isNegative) {

            totaldata += threshoid ;

        }

        for (inti =0;i <8;i++) {

            intdata = totaldata%16;

            totaldata /=16;

            switch(data) {

                case10:

                    hexString = [NSString stringWithFormat:@"A%@",hexString] ;

                    break;

                case11:

                    hexString = [NSStringstringWithFormat:@"B%@",hexString] ;

                    break;

                case12:

                    hexString = [NSString stringWithFormat:@"C%@",hexString] ;

                    break;

                case13:

                    hexString = [NSString stringWithFormat:@"D%@",hexString] ;

                    break;

                case14:

                    hexString = [NSString stringWithFormat:@"E%@",hexString] ;

                    break;

                case15:

                    hexString = [NSString stringWithFormat:@"F%@",hexString] ;

                    break;

                default:

                    hexString = [NSString stringWithFormat:@"%i%@",data,hexString] ;

                    break;

            }

        }

        while(hexString.length<2) {

            hexString = [NSStringstringWithFormat:@"0%@",hexString] ;

        }

        returnhexString ;

    }

    + (NSString*)getHexStringfromInt:(int)data

    {

        inttotaldata = data ;

        NSString*hexString =@"";

        while (totaldata >0)

        {

            intsingle = totaldata%16;

            totaldata /=16;

            switch(single) {

                case10:

                    hexString = [NSString stringWithFormat:@"A%@",hexString] ;

                    break;

                case11:

                    hexString = [NSString stringWithFormat:@"B%@",hexString] ;

                    break;

                case12:

                    hexString = [NSStringstringWithFormat:@"C%@",hexString] ;

                    break;

                case13:

                    hexString = [NSString stringWithFormat:@"D%@",hexString] ;

                    break;

                case14:

                    hexString = [NSStringstringWithFormat:@"E%@",hexString] ;

                    break;

                case15:

                    hexString = [NSString stringWithFormat:@"F%@",hexString] ;

                    break;

                default:

                    hexString = [NSString stringWithFormat:@"%i%@",single,hexString] ;

                    break;

            }

        }

        while(hexString.length<2) {

            hexString = [NSString stringWithFormat:@"0%@",hexString] ;

        }

        return hexString ;

    }

    + (int)getIntValue:(NSString*)hexString

    {

        int value =0;

        NSScanner *scanner = [NSScanner scannerWithString:hexString] ;

        unsigned long long longValue ;

        [scanner scanHexLongLong: &longValue] ;

        value = (int)longValue ;

        return value ;

    }

    + (NSData*)getData:(NSString*)hexString andSplit:(NSString*)splitSign

    {

        NSMutableData *multidata = [[NSMutableData alloc] init] ;

        NSArray *array = [hexString componentsSeparatedByString: splitSign] ;

        for(NSString*datainarray)  {

            intidata = [self getIntValue: data] ;

            [multidata appendBytes:&idata length:1] ;

        }

        returnmultidata ;

    }

    @end

    相关文章

      网友评论

          本文标题:数据转换

          本文链接:https://www.haomeiwen.com/subject/ppqdoqtx.html