16进制转nsdata ios_ios nsdata转16进制 - CSDN
精华内容
参与话题
  • iOS 16进制字符与NSData之间的转换

    千次阅读 2018-11-15 17:26:02
    #pragma mark - 十六进制转换工具 // 16进制转NSData - (NSData *)convertHexStrToData:(NSString *)str { if (!str || [str length] == 0) { return nil; } NSMutableData *hexData = [[NSMutableDa...
    #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.
    }

     

    展开全文
  • 其中将十六进制字符串转换成NSData的代码如下: - (NSData *)convertHexStrToData:(NSString *)str { if (!str || [str length] == 0) { return nil; }   NSMutableData *hexData = [...

    其中将十六进制字符串转换成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 < [strlength]; i+= 2){

    • unsignedintanInt;

    • NSString*hexCharStr= [strsubstringWithRange:range];

    • NSScanner*scanner= [[NSScanneralloc] initWithString:hexCharStr];

    •  
    • [scannerscanHexInt:&anInt];

    • NSData*entity= [[NSDataalloc] initWithBytes:&anIntlength:1];

    • [hexDataappendData:entity];

    •  
    • range.location+= range.length;

    • range.length= 2;

    • }

    •  
    • LEDEBUG(@"hexdata:%@", hexData);

    • returnhexData;

    • }

     

    将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= [NSStringstringWithFormat:@"%x", (dataBytes[i]) & 0xff];

    • if([hexStrlength] == 2){

    • [stringappendString:hexStr];

    • } else{

    • [stringappendFormat:@"0%@", hexStr];

    • }

    • }

    • }];

    •  
    • returnstring;

    • }

    •  

     

    展开全文
  • //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned long red = strtoul([str UTF8String],0,16); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六...

    NSString *str = @"0xff055008";
    //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型
    unsigned long red = strtoul([str UTF8String],0,16);
    //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六进制的,这样写也可以:
    unsigned long red = strtoul([@"0x6587" UTF8String],0,0);
    NSLog(@"转换完的数字为:%lx",red);

    // 十六进制转换为普通字符串的。 
    + (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] autorelease]; 
    [scanner scanHexInt:&anInt]; 
    myBuffer[i / 2] = (char)anInt; 
    } 
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4]; 
    NSLog(@"------字符串=======%@",unicodeString); 
    return unicodeString; 
     
     
    } 
     
    //普通字符串转换为十六进制的。 
     
    + (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; 
    } 

    //int 转data
    int i = 1; 
    NSData *data = [NSData dataWithBytes: &i length: sizeof(i)]; 
    //data 转int
    int i; 
    [data getBytes: &i length: sizeof(i)]; 


    展开全文
  • 本文记录的是使用OC进行NSData和十六进制字符串之间的相互转换。其中将十六进制字符串转换成NSData的代码如下:

    欢迎到个人博客: liumh.com查看本文。

    本文为原创,未经允许勿转载


    本文记录的是使用OC进行NSData和十六进制字符串之间的相互转换。其中将十六进制字符串转换成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;
    }

    如果传入参数 str1ff, 则hexdata打印出:hexdata: <01ff>

    将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;
    }

    将第一个例子中生成的NSData当做入参,则返回的string为01ff


    如果觉得本文对你有帮助,就请用微信打赏我吧^_^

    请叶秋喝杯咖啡吧

    展开全文
  • iOS16数据转换成2进制字符串

    千次阅读 2015-10-25 20:29:00
    下面这个是将16进制转换成2进制数据 + (NSData *)hex2data:(NSString *)hex { NSMutableData *data = [NSMutableData dataWithCapacity:hex.length / 2]; unsigned char whole_byte; char byte_chars[3] = {'\0'...
  • object-c 继承于c语言,因此最基础的数据之间的转换自然也成为oc...1.NSData 与 NSStringNSDate->NSStringNSString *testString =[[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding]; NSStr
  • + (NSData *) stringToHexData:(NSString *)hexStr {  int len = [hexStr length] / 2; // Target length  unsigned char *buf = malloc(len);  unsigned char *whole_byte = buf;  char byte_c
  • 进制流对于我们程序员来说不陌生,但是看起来那是相当困难,近期学习了一些进制的转换,希望对大家有用/** * 将二进制数据转换成十六进制字符串 * * @param data 二进制数据 * * @return 十六进制字符串 */ + ...
  • iOS 二进制数组16进制字符串

    千次阅读 2015-08-28 16:02:37
    iOS 二进制数组16进制字符串 Byte bytes[28]; [data getBytes:bytes length:28]; NSMutableString *hexString = [[NSMutableString alloc] init]; for (int i = 0; i ; i++) {
  • 16进制NSString与NSData

    千次阅读 2016-03-09 17:29:03
    - (NSData*)dataFormHexString:(NSString*)hexString; - (NSString *)hexStringFromData:(NSData*)data;
  • 1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding]; NSString->NSData NSString *aString = @"1234abcd"; NSData *...
  • UIImage *licenseImg = [self.licenseImgAry objectAtIndex:0]; NSData *licenseData = UIImageJPEGRepresentation(licenseImg, 0.7f); Byte *bytes = (Byte *)[licenseDat
  • IOS上传图片有很多方法,一种是通过IOS提交NSData数据。 NSData传过来的数据是16进制的比如 &lt;ffd8ffe0 00104a46 49460001&gt;。 服务器得到16进制数据,要转换成二进制,才能写到图片。php语言,16...
  • 本文章作为一个ios的入门教程,将不会长篇大论介绍其中的概念及理论。而是通过案例实践来学习ios开发。 NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adata encoding:...
  • 最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回...1. int类型转16进制hexstring //int 转16进制 - (NSString *)hexFromInt:(NSInteger)val { return [NSString stringWithFormat:@"%X
  • iOS NSData与long long的互

    千次阅读 2017-09-06 13:13:30
    #pragma mark - NSData转long long - (long long) NSDataToUInt:(NSData *)data { long long datatemplength; [data getBytes:&datatemplength length:sizeof(datatemplength)]; long long result = CFSwapI
  • iOS 蓝牙 字符串bytedata

    千次阅读 2017-06-04 12:27:33
    需求:字符串AD4532692D30AF836C75FA343ABC5A7E 转为16位的data :原理 先转为byte 再转为data第一种方法:- (void)strToData{ NSString *strtest =@"AF3067692D30AF836C75FA535ABC9A3E"; //以及初始化的Byte数组 ...
  • 1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding]; NSString->NSData NSString *aString = @"1234...
  • 16进制字符串转换成NSData: -(NSData *)hexString:(NSString *)hexString { int j=0; Byte bytes[20]; ///3ds key的Byte 数组, 128位 for(int i=0; i<[hexString length]; i++) { int int_ch; //
1 2 3 4 5 ... 20
收藏数 3,058
精华内容 1,223
关键字:

16进制转nsdata ios