iOS NSData与long long的互转

NSData转long long

#pragma mark - NSData转long long
- (long long) NSDataToUInt:(NSData *)data
{
    long long datatemplength;

    [data getBytes:&datatemplength length:sizeof(datatemplength)];

    long long result = CFSwapInt64BigToHost(datatemplength);//大小端不一样,需要转化

    return result;
}

long long转NSData

#pragma mark - long long转NSData
- (NSData *)longToNSData:(long long)data
{

    long long datatemplength = CFSwapInt64BigToHost(data);  //大小端不一样,需要转化

    NSData *temdata = [NSData dataWithBytes: &datatemplength length: sizeof(datatemplength)];

    return temdata;
}

10进制转化为二字节16进制字符串

+(NSString *)ToHex:(long long int)tmpid

{

    NSString *nLetterValue;

    NSString *str =@"";

    long long int ttmpig;

    for (int i =0; i<9; i++) {

        ttmpig=tmpid%16;

        tmpid=tmpid/16;

        switch (ttmpig)

        {

            case 10:

                nLetterValue =@"A";break;

            case 11:

                nLetterValue =@"B";break;

            case 12:

                nLetterValue =@"C";break;

            case 13:

                nLetterValue =@"D";break;

            case 14:

                nLetterValue =@"E";break;

            case 15:

                nLetterValue =@"F";break;

            default:nLetterValue=[[NSString alloc] initWithFormat:@"%lli",ttmpig];



        }

        str = [nLetterValue stringByAppendingString:str];

        if (tmpid == 0) {

            break;

        }



    }

    return str;

}

16NSString TO 10NSInteger

+ (NSInteger)stringToNSIntegerWithString:(NSString *)str
{
    NSInteger temInt = strtoul([str UTF8String], 0, 16);

    return temInt;
}

NSData To NSInteger

+ (NSInteger)dataToInteger:(NSData *)data
{
    NSString *temStr = [NSData HexStringWithData:data];


    return [temStr integerValue];
}

将二进制数据转化为字符表示的16进制数

#pragma mark - 将二进制数据转化为字符表示的16进制数
+ (NSString *)HexStringWithData:(NSData *)data
{
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];

    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];

    return string;
}

将字符表示的16进制数转化为二进制数据

#pragma mark - 将字符表示的16进制数转化为二进制数据
+ (NSMutableData *)dataWithHexString:(NSString *)hexString
{

    if (!hexString || [hexString length] == 0) {
        return nil;
    }

    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([hexString length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [hexString length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [hexString substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];

        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];

        range.location += range.length;
        range.length = 2;
    }

    //   FMLog(@"hexdata: %@", hexData);
    return hexData;

}

字符串补零操作

+ (NSString *)addZero:(NSString *)str withLength:(NSInteger)length{

    NSString *string = nil;

    if (str.length==length) {

        return str;

    }

    if (str.length<length) {

        NSUInteger inter = length-str.length;

        for (int i=0;i< inter; i++) {

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

            str = string;

        }

    }

    return string;

}

你可能感兴趣的:(技术笔记)