2018-11-15 17:26:02 qingshuiputeng 阅读数 1252
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    41493 人正在学习 去看看 欧阳坚
#pragma mark - 十六进制转换工具
// 16进制转NSData
- (NSData *)convertHexStrToData:(NSString *)str
{
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str 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;
    }
    return hexData;
}
// NSData转16进制 第一种
- (NSString *)convertDataToHexStr:(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;
}
//普通字符串转换为十六进制的。
- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}
// 十六进制转换为普通字符串的。
- (NSString *)stringFromHexString:(NSString *)hexString {
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    return unicodeString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

2016-09-27 11:50:25 shb2058 阅读数 700
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    41493 人正在学习 去看看 欧阳坚

//data16

+(NSString *)DataToHexStr:(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禁止转data

+(NSData *)HexStrToData:(NSString *)str {

    if (!str || [str length] == 0) {

        return nil;

    }

    

    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];

    NSRange range;

    if ([str length] % 2 == 0) {

        range = NSMakeRange(0, 2);

    } else {

        range = NSMakeRange(0, 1);

    }

    for (NSInteger i = range.location; i < [str length]; i += 2) {

        unsigned int anInt;

        NSString *hexCharStr = [str 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;

    }


    return hexData;

}

2015-12-31 15:55:33 iOSbird 阅读数 471
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    41493 人正在学习 去看看 欧阳坚

将16进制字符串转换成16进制数,返回NSData类型的数据


- (NSData *)transformSignString:(NSString *)hexString{


    int j=0;

    NSInteger desLength = hexString.length/2;

    if (hexString.length%2 != 0) {

        

        return nil;

    }

    Byte bytes[desLength]; ///3ds keyByte 数组, 128

    for(int i=0;i<[hexString length];i++)

    {

        int int_ch; /// 两位16进制数转化后的10进制数

        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

        int int_ch1;

        if(hex_char1 >= '0' && hex_char1 <='9')

            int_ch1 = (hex_char1-48)*16;   //// 0 Ascll - 48

        else if(hex_char1 >= 'A' && hex_char1 <='F')

            int_ch1 = (hex_char1-55)*16; //// A Ascll - 65

        else

            int_ch1 = (hex_char1-87)*16; //// a Ascll - 97

        i++;

        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

        int int_ch2;

        if(hex_char2 >= '0' && hex_char2 <='9')

            int_ch2 = (hex_char2-48); //// 0 Ascll - 48

        else if(hex_char2 >= 'A' && hex_char2 <='F')

            int_ch2 = hex_char2-55; //// A Ascll - 65

        else

            int_ch2 = hex_char2-87; //// a Ascll - 97

        

        

        int_ch = int_ch1+int_ch2;

        NSLog(@"int_ch=%d",int_ch);

        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里

        j++;

    }

    NSData *newData = [[NSData alloc] initWithBytes:bytes length:desLength];

    NSLog(@"newData=%@",newData);

    return newData;

}

2016-11-24 15:57:17 dkq972958298 阅读数 7416
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    41493 人正在学习 去看看 欧阳坚

其中将十六进制字符串转换成NSData的代码如下:

- (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str 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;
    }
    
    LEDEBUG(@"hexdata: %@", hexData);
    return hexData;
}

将NSData转换成十六进制的字符串则可使用如下方式:


- (NSString *)convertDataToHexStr:(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;
}





2012-12-20 17:43:28 Dwarven 阅读数 9041
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    41493 人正在学习 去看看 欧阳坚
- (NSData*)dataFormHexString:(NSString*)hexString{
    hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (!(hexString && [hexString length] > 0 && [hexString length]%2 == 0)) {
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}

- (NSString *)hexStringFromData:(NSData*)data{
    return [[[[NSString stringWithFormat:@"%@",data]
              stringByReplacingOccurrencesOfString: @"<" withString: @""]
             stringByReplacingOccurrencesOfString: @">" withString: @""]
            stringByReplacingOccurrencesOfString: @" " withString: @""];
}

iOS 16进制转成汉字

阅读数 644

没有更多推荐了,返回首页